When is a Java Class loaded?

There is no simple answer to this question. The specification leaves room for different implementation strategies and even within one implementation, it will depend on how the class is been used. Further, your question is more about “when can it fail” which depends on why it may fail, e.g. a class may be loaded at … Read more

how to set java class loader PARENT_LAST

If you are only deploying the WAR file itself you can’t control this, but if you have your WAR file in an EAR file you can use the deployment.xml solution. The deployment.xml file would look something like this: <?xml version=”1.0″ encoding=”UTF-8″?> <appdeployment:Deployment xmi:version=”2.0″ xmlns:xmi=”http://www.omg.org/XMI” xmlns:appdeployment=”http://www.ibm.com/websphere/appserver/schemas/5.0/appdeployment.xmi” xmi:id=”Deployment_1347529484613″> <deployedObject xmi:type=”appdeployment:ApplicationDeployment” xmi:id=”ApplicationDeployment_1347544766353″ startingWeight=”99″ warClassLoaderPolicy=”SINGLE”> <modules xmi:type=”appdeployment:WebModuleDeployment” xmi:id=”WebModuleDeployment_1347543866613″ startingWeight=”1″ … Read more

How to replace classes in a running application in java ?

Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the file system and calls Test1.hello() package test; public class Test1 { public void hello() { System.out.println(“Hello !”); } } public class Test { static class TestClassLoader extends ClassLoader { @Override public Class<?> loadClass(String name) throws ClassNotFoundException { if (name.equals(“test.Test1”)) { try { … Read more

Static references are cleared–does Android unload classes at runtime if unused?

Both you (@Matthias) and Mark Murphy (@CommonsWare) are correct in what you say, but the gist seems lost. (The use of volatile is correct and classes are not unloaded.) The crux of the question is where initialize is called from. Here is what I think is happening: You are calling initialize from an Activity * … Read more

Java security: Sandboxing plugins loaded via URLClassLoader

From the docs: The AccessControlContext of the thread that created the instance of URLClassLoader will be used when subsequently loading classes and resources. The classes that are loaded are by default granted permission only to access the URLs specified when the URLClassLoader was created. The URLClassLoader is doing exactly as its says, the AccessControlContext is … Read more

Flow of class loading for a simple program

You will run your Sample class as follows > java Sample for little magic, check out the output of-verbose:class option and you see tons of following lines.. [Opened C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.lang.Object from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.io.Serializable from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded java.lang.Comparable from C:\jdk1.6.0_14\jre\lib\rt.jar] . . . . . . [Loaded java.security.cert.Certificate from C:\jdk1.6.0_14\jre\lib\rt.jar] [Loaded Sample from file:/D:/tmp/] … Read more

Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger

During runtime your application is unable to find the jar. Taken from this answer by Jared: It is important to keep two different exceptions straight in our head in this case: java.lang.ClassNotFoundException This an Exception, it indicates that the class was not found on the classpath. This indicates that we were trying to load the … Read more