使用 URLClassLoader 加载 Jar 的 ClassNotFound
ClassNotFound with URLClassLoader loading a Jar
使用下面的代码我得到一个 ClassNotFoundException
File jarFile = new File(jar);
URL jarUrl = new URL("jar", "", jarFile.getAbsolutePath() + "!/");
URLClassLoader loader = new URLClassLoader(
new URL[] {jarUrl}, ClassLoader.getSystemClassLoader());
Class<? extends Animal> clazz =
(Class<? extends Animal>)loader.loadClass("com.mycompany.dog.Dog");
Jar的内容如下:
META-INF/
META-INF/MANIFEST.MF
com/
com/mycompany/
com/mycompany/dog/
com/mycompany/dog/Dog.class
com/mycompany/dog/Dog.class
META-INF/maven/
META-INF/maven/com.mycompany.app/
META-INF/maven/com.mycompany.app/dog/
META-INF/maven/com.mycompany.app/dog/pom.xml
META-INF/maven/com.mycompany.app/dog/pom.properties
com/mycompany/app/
com/mycompany/app/Animal.class
com/mycompany/app/ActionLog.class
META-INF/maven/com.mycompany.app/animal/
META-INF/maven/com.mycompany.app/animal/pom.xml
META-INF/maven/com.mycompany.app/animal/pom.properties
我是不是做错了什么?
我不相信尝试通过 URL 构造函数创建 Jar URL,因为它可能不同于调用 File.toURI().toURL()
生成的 URL(它形成一个有效的 URL)。
这是一个例子。
import java.net.URL;
import java.io.File;
public class JarURL {
public static void main(String[] args) throws Exception {
File f = new File("C:\the.jar");
URL jarUrl1 = new URL("jar", "", f.getAbsolutePath() + "!/");
URL jarUrl2 = f.toURI().toURL();
System.out.println("jarUrl1: " + jarUrl1);
System.out.println("jarUrl2: " + jarUrl2);
}
}
产生输出:
jarUrl1: jar:C:\the.jar!/
jarUrl2: file:/C:/the.jar
使用下面的代码我得到一个 ClassNotFoundException
File jarFile = new File(jar);
URL jarUrl = new URL("jar", "", jarFile.getAbsolutePath() + "!/");
URLClassLoader loader = new URLClassLoader(
new URL[] {jarUrl}, ClassLoader.getSystemClassLoader());
Class<? extends Animal> clazz =
(Class<? extends Animal>)loader.loadClass("com.mycompany.dog.Dog");
Jar的内容如下:
META-INF/
META-INF/MANIFEST.MF
com/
com/mycompany/
com/mycompany/dog/
com/mycompany/dog/Dog.class
com/mycompany/dog/Dog.class
META-INF/maven/
META-INF/maven/com.mycompany.app/
META-INF/maven/com.mycompany.app/dog/
META-INF/maven/com.mycompany.app/dog/pom.xml
META-INF/maven/com.mycompany.app/dog/pom.properties
com/mycompany/app/
com/mycompany/app/Animal.class
com/mycompany/app/ActionLog.class
META-INF/maven/com.mycompany.app/animal/
META-INF/maven/com.mycompany.app/animal/pom.xml
META-INF/maven/com.mycompany.app/animal/pom.properties
我是不是做错了什么?
我不相信尝试通过 URL 构造函数创建 Jar URL,因为它可能不同于调用 File.toURI().toURL()
生成的 URL(它形成一个有效的 URL)。
这是一个例子。
import java.net.URL;
import java.io.File;
public class JarURL {
public static void main(String[] args) throws Exception {
File f = new File("C:\the.jar");
URL jarUrl1 = new URL("jar", "", f.getAbsolutePath() + "!/");
URL jarUrl2 = f.toURI().toURL();
System.out.println("jarUrl1: " + jarUrl1);
System.out.println("jarUrl2: " + jarUrl2);
}
}
产生输出:
jarUrl1: jar:C:\the.jar!/
jarUrl2: file:/C:/the.jar