默认classloader只会加载其中一个,想要使用另外一个类,需要手动实现一个自定义的classloader
- example:
Let's say we have two different classes with the same name, in two different Jars:
package example.library;
public final class SomeClass {
private final int a, b;
public SomeClass(int a, int b) {
this.a = a;
this.b = b;
}
public void doSomething() {
System.out.printf("Do something with %d and %d.%n", a, b);
}
}
package example.library;
public final class SomeClass {
private final String s;
public SomeClass(String s) {
this.s = s;
}
//拥有一个不同的方法
public void doSomethingElse(boolean yes) {
if (yes)
System.out.println("Say yes to " + s);
else
System.out.println("Say no to " + s);
}
}
The first one is located in Lib1.jar, the second one is located in Lib2.jar. Both are in the classpath.
In my case, I can't use the second class, because the first one is being loaded by the class loader:
package example;
import example.library.SomeClass;
final class Test {
public static void main(String... args) {
SomeClass instance = new SomeClass("Java");
instance.doSomethingElse(true);
}
}
Invoking this program will give me a NoSuchMethodException, if I get it to compile in the first place. So if I want to use the second class, I have to write a wrapper class for it:
package example;
import java.io.*;
import java.lang.reflect.*;
import java.util.jar.*;
final class MyClassWrapper {
private final Object delegate;
private MyClassWrapper(Object delegate) {
this.delegate = delegate;
}
void doSomethingElse(boolean yes) {
try {
delegate.getClass().getMethod("doSomethingElse", boolean.class).invoke(delegate, yes);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof RuntimeException)
throw (RuntimeException) cause;
else if (cause instanceof Error)
throw (Error) cause;
else
throw new AssertionError(ex);
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
static final class Factory {
private final Class<?> clazz;
Factory(final JarFile jar) throws ClassNotFoundException {
clazz = Class.forName("example.library.SomeClass", true, new ClassLoader(null) {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String entryName = name.replaceAll("[.]", "/") + ".class";
JarEntry entry = jar.getJarEntry(entryName);
if (entry == null)
throw new ClassNotFoundException("Jar entry not found");
byte[] data = new byte[(int) entry.getSize()];
InputStream in = null;
try {
in = jar.getInputStream(entry);
in.read(data);
return defineClass(name, data, 0, data.length);
} catch (IOException ex) {
throw new ClassNotFoundException("Exception while reading Jar entry", ex);
} finally {
if (in != null)
try {
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
}
MyClassWrapper build(String s) {
try {
Object instance = clazz.getConstructor(String.class).newInstance(s);
return new MyClassWrapper(instance);
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof RuntimeException)
throw (RuntimeException) cause;
else if (cause instanceof Error)
throw (Error) cause;
else
throw new AssertionError(ex);
} catch (Exception ex) {
throw new AssertionError(ex);
}
}
}
}
This wrapper provides exactly the same methods as the class I want to use, and it provides a factory class that can provide instances of the wrapper class. Using this class works as follows:
package example;
import java.io.*;
import java.util.jar.*;
final class Test {
public static void main(String... args) throws Exception {
JarFile jar = new JarFile(new File("Lib2.jar"));
try {
MyClassWrapper.Factory factory = new MyClassWrapper.Factory(jar);
MyClassWrapper instance = factory.build("Java");
instance.doSomethingElse(true);
} finally {
jar.close();
}
}
}
网友评论