Make JVM use My Own Class Loader
I wrote my own class loader. I need all my classes to be loaded using my class loader. I have passed the VM the following: -Djava.system.class.loader=MyClassLoader
Only the first referenced class in my package is being loaded using my class loader. Other classes in my package are being loaded using AppClassLoader
.
Here is what MyClassLoader
looks like:
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
public class MyClassLoader extends ClassLoader {
public MyClassLoader() {
super(MyClassLoader.class.getClassLoader());
}
public MyClassLoader(ClassLoader parent) {
super(parent);
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
System.out.println("MyClassLoader is loading " + name);
return super.loadClass(name);
}//loadClass
@Override
public synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
System.out.println("MyClassLoader is loading " + name + " with resolve = " + resolve);
return super.loadClass(name, resolve);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
System.out.println("MyClassLoader findClass " + name);
return super.findClass(name);
}
@Override
protected URL findResource(String name) {
System.out.println("MyClassLoader findResource " + name);
return super.findResource(name);
}
@Override
protected Enumeration<URL> findResources(String name) throws IOException {
System.out.println("MyClassLoader findResources " + name);
return super.findResources(name);
}
@Override
protected Package getPackage(String name) {
System.out.println("MyClassLoader getPackage " + name);
return super.getPackage(name);
}
@Override
public URL getResource(String name) {
System.out.println("MyClassLoadergetResource " + name);
return super.getResource(name);
}
@Override
public InputStream getResourceAsStream(String name) {
System.out.println("MyClassLoader getResourceAsStream " + name);
return super.getResourceAsStream(name);
}
@Override
public Enumeration<URL> getResources(String name) throws IOException {
System.out.println("MyClassLoader getResources " + name);
return super.getResources(name);
}
}//MyClassLoader
I have this code in a static block in one of my classes that are being loaded:
static {
System.out.println("My class loader = " + MyClass.class.getClassLoader().getClass());
}
The output of this was:
My class loader = class sun.misc.Launcher$AppClassLoader
Is there a way that I can force using my class loader?
Based on what I've read, what you have done should work. I suspect that the problem may be in your classloader. Another possibility is that you haven't used the fully qualified classname of your classloader.
(I'm puzzled by what you mean by the "AppClass loader". Perhaps you could elaborate in an edit to the question.)
For the record, the javadoc for the ClassLoader.getSystemClassLoader()
method explains what the java.system.class.loader
property actually does. Read it and see if that gives any clues to the cause of your problems.
上一篇: 计算表达式模n
下一篇: 让JVM使用我自己的类加载器