classloader

Java Class.forName() from distant directory

折月煮酒 提交于 2019-11-26 14:41:33
问题 I am currently loading Java classes using Class.forName() to load it. clazz = Class.forName("interfaces.MyClass"); But now I want to load classes from different directory, I have tried to set classpath by clazz = Class.forName("-cp \"C:/dir\" distantinterfaces.DistantClass"); With no success and ClassNotFoundException . Full path to distant class is: C:/dir/distantinterfaces/DistantClass.class 回答1: Use an URLClassLoader for this. The code might be something along the lines of: File f = new

Java Dynamically Loading a class

喜欢而已 提交于 2019-11-26 14:29:39
问题 I am attempting to load classes dynamically into a component. I am using a file chooser to select the .JAR file that will be loaded and then a option pane to get the name of the class. I have trawled the internet looking for how to convert a java file to a URL in order to load it in URLClassLoader and I have come up with: File myFile = filechooser.getSelectedFile(); String className = JOptionPane.showInputDialog( this, "Class Name:", "Class Name", JOptionPane.QUESTION_MESSAGE); URL myUrl=

ClassCastException because of classloaders?

依然范特西╮ 提交于 2019-11-26 14:23:55
问题 While playing with classloaders i got the following exception: Exception in thread "main" java.lang.ClassCastException: xxx.Singleton cannot be cast to xxx.Singleton Does this mean that an instance from a classloader is not castable to an class of another classloader? Check my code where i'm able to instanciate 3 singletons thanks to classloaders, even with the "" security. public static void main(String[] args) throws Exception { URL basePath = new URL("file:/myMavenPath/target/classes/");

this.getClass().getClassLoader().getResource(“…”) and NullPointerException

早过忘川 提交于 2019-11-26 14:11:04
I have created a minimal maven project with a single child module in eclipse helios. In the src/test/resources folder I have put a single file "install.xml". In the folder src/test/java I have created a single package with a single class that does: @Test public void doit() throws Exception { URL url = this.getClass().getClassLoader().getResource("install.xml"); System.out.println(url.getPath()); } but when I run the code as a junit 4 unit test I just get a NullPointerException. This has worked fine a million of times before. Any ideas? I have followed this guide: http://www.fuyun.org/2009/11

Singleton class with several different classloaders

廉价感情. 提交于 2019-11-26 13:19:57
E.g I have class Singleton with static field instance : public class Singleton { private static Singleton instance; // other code, construct, getters, no matter } I can load this class twice with two different classloaders. How could I avoid it? It is unsafe and dangerous. Also, if I set instance to null, would it set to null for both classes? Singleton singleton = Singleton.getInstance(); singleton = null; If you want a true Singleton across classloaders, then you need a common parent to load the class in question, or you need to specify the classloader yourself. Update: From the comment from

Java verbose class loading

拜拜、爱过 提交于 2019-11-26 12:39:14
问题 I am trying to list the order in which the Java class loader is loading my classes. if I use -verbose parameter it will list every single interface/class it loads, including tons of interfaces such as Serializable, exceptions etc. Is there a way to tweak this output so it only shows which classes are loaded in the class my main method is defined? 回答1: I guess your best bet is to do the following: Output some fixed text once your main method starts and right before it ends. Pipe the verbose

What is a Java ClassLoader?

断了今生、忘了曾经 提交于 2019-11-26 12:33:49
问题 In a few simple sentences, what is a Java ClassLoader, when is it used and why? OK, I read a wiki article. ClassLoader loads classes. OK. So if I include jar files and import, a ClassLoader does the job. Why should I bother with this ClassLoader? I\'ve never used it and didn\'t know it existed. The question is, why does the ClassLoader class exist? And also, how do you use it in practice? (Cases exist, I know.) 回答1: Taken from this nice tutorial from Sun: Motivation Applications written in

Determine which JAR file a class is from

為{幸葍}努か 提交于 2019-11-26 12:02:21
I am not in front of an IDE right now, just looking at the API specs. CodeSource src = MyClass.class.getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); } I want to determine which JAR file a class is from. Is this the way to do it? Chandra Patni Yes. It works for all classes except classes loaded by bootstrap classloader. The other way to determine is: Class klass = String.class; URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class"); As notnoop pointed out klass.getResource() method returns the location of the class file itself

How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?

此生再无相见时 提交于 2019-11-26 11:40:47
I would like to create a parent-last / child-first class loader, e.g. a class loader that will look for classes in the child class loder first, and only then delegate to it's parent ClassLoader to search for classes. Clarification: I know now that to get complete ClassLoading seperation I need to use something like a URLClassLoader passing null as it's parent, thanks to this answer to my previous question However the current question comes to help me resolve this issue: My code + dependent jars are being loaded into an existing system, using a ClassLoader that sets that System's ClassLoader as

Java : in what order are static final fields initialized?

 ̄綄美尐妖づ 提交于 2019-11-26 11:20:18
问题 Okay, so say I have a class that looks like this : public class SignupServlet extends HttpServlet { private static final Logger SERVLET_LOGGER=COMPANYLog.open(SignupServlet.class); private static final ExceptionMessageHandler handler = new ExceptionMessageHandler(); private static final SignupServletObservableAgent signupObservableAgent = new SignupServletObservableAgent(null, SERVLET_LOGGER); } Can I count on the class loader to initialize those fields in order, such that I can rely on