使用URLClassLoader加载Jar的ClassNotFound

时间:2018-03-17 02:32:58

标签: java maven url classnotfoundexception urlclassloader

使用以下代码我得到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$1.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

我做错了吗?

1 个答案:

答案 0 :(得分:2)

我不相信尝试通过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