java的包名分开了怎么回事_java中两个出现两个完全相同的包名和类名解决方法...

默认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();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值