我正在尝试加载外部jar并从该jar创建所有类的结构。
目前,jar正在使用对repo上不同位置的依赖。
Accessed jar位于someurl / com / xyz / mystuff2.jar 访问jar mystuff1.jar依赖于someurl / com / abc / mystuff1.jar。
我尝试了以下内容。
try {
u = new URL("jar", "", this.url /* Some url*/+ "!/");
URL[] urls = { u };
URLClassLoader cl = URLClassLoader.newInstance(urls);
Enumeration<JarEntry> entries = this.jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
/* Soft checks to make sure it is not pulling an inner class
*/
if (entry.isDirectory() || !entry.getName().endsWith(".class")
&& !entry.getName().contains("$")) {
continue;
}
String className = entry.getName().substring(0,
entry.getName().length() - 6);
className = className.replace('/', '.');
try {
@SuppressWarnings("unused")
Class<?> c = cl.loadClass(className); /*It errors while
loading a class that are extending classes from mystuff.jar*/
} catch (ClassNotFoundException e) {
logger.error("ERROR: Classes were not loaded properly "
+ e.getMessage());
} catch (NoClassDefFoundError e) {
logger.error("ERROR: Classes definition was not found "
+ e.getMessage());
}
}
} catch (MalformedURLException e) {
logger.error("ERROR : There is an issue while accessing the
URL"+ e.getMessage());
}
我肯定在这里错过了一点,因为jar已经编译和部署,它应该已经有来自mystuff1的绑定了。此外,鉴于扩展类是抽象的,可以选择加载类或了解它们。
我的目标是能够通过反射检索所有类,扩展我的BaseClass.class的类,并执行创建一些有意义的结构。
编辑:
使用过的别名。
java.lang.NoClassDefFoundError: com/myorg/xyz/core/BaseClass
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:789)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.myorg.xyz.gotto.go.repomanager.MavenRepoManager.loadClassesFromJar(MavenRepoManager.java:97)
at com.myorg.xyz.gotto.go.RepoManagerTest.testRepoManager(RepoManagerTest.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.ClassNotFoundException: com.myorg.xyz.core.BaseClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:789)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 38 more