In a previous article of this series we learned how to replace the default system class loader for any given class. This time we will show how to replace it even before running the main method, so that the entire program runs from start with a custom class loader.
To do it, you should change the “java.system.class.loader” java environment variable to the full name of the class loader you want to use when running the program. Let’s create a simple program to demonstrate it. It will print out its own class loader:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: |
public class SimpleMain {
/**
* If you run this main method supplying the
* -Djava.system.class.loader=javablogging.CustomClassLoader
* parameter, class SimpleMain will be loaded with
* our CustomClassLoader. Every other
* class referenced from here will be also loaded with it.
*/
public static void main(String... strings) {
System.out.print("This is my ClassLoader: "
+ SimpleMain.class.getClassLoader());
}
}
|
Run this program the following way:
java -Djava.system.class.loader
=javablogging.CustomClassLoader javablogging.SimpleMain
|
You will get the following output, proving that our classloader is active from the beginning:
loading class 'java.lang.System' loading class 'java.nio.charset.Charset' loading class 'java.lang.String' loading class 'javablogging.SimpleMain' loading class 'java.lang.Object' loading class 'java.lang.StringBuilder' loading class 'java.lang.Class' loading class 'java.io.PrintStream' This is my ClassLoader: javablogging.CustomClassLoader@42e816 |
As you can see, class javablogging.SimpleMain and all classes used in it were loaded with our CustomClassLoader. In contrary to the way in the previous article we did not have to specifically create now an instance of our class loader inside the code.
6 Comments until now
[...] This post was mentioned on Twitter by Rodrigo Garcia. Rodrigo Garcia said: Java ClassLoader (4) – Loading a custom ClassLoader on JVM start http://bit.ly/9KLGIg [...]
Cool. Thx. Short and sweet
It seems not working for me, but maybe something has changed since May 16 2010:
This is my ClassLoader: sun.misc.Launcher$AppClassLoader@6d6f0472
I’ve a larger project with the same issue, I’m posting here as this seems to me the perfect minimal testcase that shows the problem
PS: In your SimpleMain if I print ClassLoader.getSystemClassLoader() I have your CustomClassLoader@431b9fb1 as an output, but that’s not the same of my SimpleMain.class.getClassLoader(), as you can see, and that’s a problem
thank you if you have a solution
I’ve got same problem as Giacomo.
When set at start
-Djava.system.class.loader=CustomClassLoader
only couple classes are loaded by my CustomClassLoader, most of them (from my application) are loaded by sun.misc.Launcher$AppClassLoader@6d6f0472.
I’ve got no idea why ?
Is there any way to replace this sun AppClassLoader to CustomClassLoader ?
Thank you for this simply understandable series. please continue this good work giving more insights
I also has the same problem.
Anyone please suggest.
Add your Comment!