在运行时加载JAR文件类时出错

时间:2012-10-31 02:27:20

标签: java jar classloader classnotfoundexception urlclassloader

我正在尝试将JAR文件添加到类路径并在运行时从JAR文件加载所有类。这是我为此任务编写的代码(此类扩展URLClassLoader)

public void loadJar(final String fName) throws IOException, IllegalAccessException, ClassNotFoundException {
    final File file = new File(fName);
    if (file.exists() && getFileExtension(file.getName()).equalsIgnoreCase("jar")) {
        addURL(file.toURI().toURL());
        for(final URL url : getURLs()){
            System.out.println(url.toString());
        }
        final ZipFile jarFile = new ZipFile(file, ZipFile.OPEN_READ);
        final Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) jarFile.entries();
        while (entries.hasMoreElements()) {
            final String className = getClassCanonicalName(entries.nextElement());
            if (className != null) {
                loadClass(getClassCanonicalName(entries.nextElement()));
            }
        }
    }
}

private String getFileExtension(final String fileName) {
    return fileName.substring(fileName.lastIndexOf(".") + 1);
}

private String getClassCanonicalName(final ZipEntry entry) {
    final String entryName = entry.getName();
    if (getFileExtension(entryName).toLowerCase().endsWith("class")) {
        return entryName.replaceAll(File.separator, ".");
    } else {
        return null;
    }
}

但是我继续为类实体获取ClassNotFoundException,即使通过getURL确实指示jar文件已被添加到此加载器。

这个问题的原因是什么?感谢

1 个答案:

答案 0 :(得分:1)

return entryName.replaceAll(File.separator, "."); 

在Windows上,这将失败。对于在任何平台上制作的Zip,/的分隔符应为ZipEntry

所以用以下代码替换:

return entryName.replaceAll("/", "."); 

同时删除班级名称。 SSCCE E.G。:

import java.io.*;
import java.net.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class URLClassLoaderTest extends URLClassLoader {

    public URLClassLoaderTest(URL[] arg0) {
        super(arg0);
    }

    public void loadJar(URL urlOfJar) throws IOException, IllegalAccessException, ClassNotFoundException {
        if (getFileExtension(urlOfJar.getFile()).equalsIgnoreCase("jar")) {
            addURL(urlOfJar);
            for(final URL url : getURLs()){
                System.out.println(url.toString());
            }
            final ZipInputStream zis = new ZipInputStream(urlOfJar.openStream());
            ZipEntry ze = zis.getNextEntry();
            while (ze!=null) {
                final String className = getClassCanonicalName(ze);
                if (className != null) {
                    loadClass(getClassCanonicalName(ze));
                }
                ze = zis.getNextEntry();
            }
        }
    }

    private String getFileExtension(final String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    private String getClassCanonicalName(final ZipEntry entry) {
        final String entryName = entry.getName();
        if (getFileExtension(entryName).toLowerCase().endsWith("class")) {
            String s = entryName.substring(0,entryName.length()-6);
            s = s.replaceAll("/", ".");
            System.out.println(s);
            return s;
        } else {
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        URL[] url = {new URL("http://pscode.org/lib/mime.jar")};
        URLClassLoaderTest uclt = new URLClassLoaderTest(url);
        uclt.loadJar(url[0]);
    }
}

输出

http://pscode.org/lib/mime.jar
org.pscode.mime.MimeType$1
org.pscode.mime.MimeType$1
org.pscode.mime.MimeType$2
org.pscode.mime.MimeType$2
org.pscode.mime.MimeType
org.pscode.mime.MimeType