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

When you use this.getClass().getResource(“myFile.ext”) getResource will try to find the resource relative to the package. If you use: this.getClass().getResource(“/myFile.ext”) getResource will treat it as an absolute path and simply call the classloader like you would have if you’d done. this.getClass().getClassLoader().getResource(“myFile.ext”) The reason you can’t use a leading / in the ClassLoader path is because all … Read more

Load properties file in JAR?

The problem is that you are using getSystemResourceAsStream. Use simply getResourceAsStream. System resources load from the system classloader, which is almost certainly not the class loader that your jar is loaded into when run as a webapp. It works in Eclipse because when launching an application, the system classloader is configured with your jar as … 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

Java verbose class loading

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 output into a file Use things like less or grep to find the classes loaded between the two tags from the main method. There’s a similar question and … Read more

Is it possible to dynamically load a library at runtime from an Android application?

Sorry, I’m late and the question has already an accepted answer, but yes, you can download and execute external libraries. Here is the way I did: I was wondering whether this was feasible so I wrote the following class: package org.shlublu.android.sandbox; import android.util.Log; public class MyClass { public MyClass() { Log.d(MyClass.class.getName(), “MyClass: constructor called.”); } … Read more