我有一个maven项目,它依赖于jar" sample"版本" 2.0"。 "样本" jar版本" 2.0"包含一个名为" SampleEnum"的枚举。 SampleEnum in" 2.0"版本如下所示
public enum SampleEnum {
"HERBIVORES",
"CARNIVORES",
"OMNIVORES"
}
我想加载" SampleEnum"来自"样本" jar版本" 1.0"在上面提到的maven java项目中。 " 1.0"中的SampleEnum类版本如下所示。
public enum SampleEnum {
"HERBIVORES",
"CARNIVORES"
}
我使用以下代码加载类:
public class Test
{
public static void main(String args[]) {
try {
URLClassLoader loader1 = new URLClassLoader(new URL[] {new File("sample-1.0.jar").toURL()}, Thread.currentThread().getContextClassLoader());
Class<?> c1 = loader1.loadClass("SampleEnum");
for (Object o : c1.getEnumConstants()) {
System.out.println(o);
}
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}
上述计划的实际输出是:
食草动物 食肉动物 杂食动物
因为我已经加载了#34; sample-1.0&#34; jar我期待以下输出:
食草动物 食肉动物
我可以知道它从依赖jar而不是指定的jar加载类的原因吗?
有没有办法在maven项目中从1.0版jar加载枚举类?
答案 0 :(得分:1)
它从依赖jar加载类的原因是因为loader1
类加载器将当前Thread的上下文类加载器作为其父类加载器并且该类加载器从依赖jar加载类。公共ClassLoader.loadClass(String)
方法调用受保护的ClassLoader.loadClass(String, boolean)
方法,并记录该方法以解释始终首先搜索父类加载器。当然,您可以通过继承URLClassLoader
并覆盖loadClass(String, boolean)
方法来覆盖此行为。
org.sample.SampleEnum
public enum SampleEnum
{
HERBIVORES,
CARNIVORES
}
来自sample-2.0.jar的 org.sample.SampleEnum
public enum SampleEnum
{
HERBIVORES,
CARNIVORES,
OMNIVORES
}
Main.java
package com.example.app;
import org.sample.SampleEnum;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import static java.util.Arrays.asList;
public class Main
{
public static void main(String[] args)
{
System.out.println("v2.0: " + asList(SampleEnum.values()));
try {
URLClassLoader loader1 = new CustomURLClassLoader(
new URL[] { new File("../sample-1.0/target/sample-1.0.jar").toURL() });
Class<?> c1 = loader1.loadClass("org.sample.SampleEnum");
System.out.println("v1.0: " + asList(c1.getEnumConstants()));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
class CustomURLClassLoader extends URLClassLoader
{
public CustomURLClassLoader(URL[] urls)
{
super(urls);
}
protected Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
long t0 = System.nanoTime();
// First, look in the current ClassLoader
long t1 = System.nanoTime();
try {
c = findClass(name);
} catch (ClassNotFoundException ex) {
// ClassNotFoundException thrown if class not found
}
// this is the defining class loader; record the stats
sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
sun.misc.PerfCounter.getFindClasses().increment();
// Lastly, look in the parent ClassLoader
try {
if (getParent() != null) {
c = super.loadClass(name, resolve);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
}
}
输出:
$ java -cp sample-app-2.0.jar:sample-2.0.jar com.example.app.Main
v2.0: [HERBIVORES, CARNIVORES, OMNIVORES]
v1.0: [HERBIVORES, CARNIVORES]