2

我正在尝试将 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;
    }
}

但是,即使通过 getURLs 确实表明 jar 文件已添加到此加载器中,我仍不断收到类实体的 ClassNotFoundException。

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

4

1 回答 1

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

在 Windows 上,这将失败。它应该是在任何平台 上制作的 Zip/的分隔符。ZipEntry

因此,将其替换为:

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

还要去掉类名。SSCCE EG:

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
于 2012-10-31T02:34:48.540 回答