Load a class in a jar just with his name

2019-09-20 15:13发布

问题:

I tried to load a class within a jar, without success.

URL url = null;
    try {
        url = new URL("jar:file:"+System.getProperty("user.dir")+File.separator+"games"+File.separator+gameName+"!/");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URL[] urls = {url};
    URLClassLoader cl = URLClassLoader.newInstance(urls);
    try {
        String gameClassPath = null;
        JarFile jar = new JarFile(url.getPath().replace("file:", "").replace("!", ".jar"));
        Enumeration<JarEntry> entries = jar.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            if (entry.getName().contains(gameName)) {
                gameClassPath = entry.getName().replace('/', '.').replace(".class", "");
            }
        }
        this.gameClass = cl.loadClass(gameClassPath);
        jar.close();
    } catch (ClassNotFoundException | IOException e) {
        e.printStackTrace();
    }

I think the problem is coming from the URLClassLoader which may not be at the right place.

Editing the question with the OP's updates[posted in answer section]

I still have error with @spiderman version. I precise that gameClass type is Class<?> , not Class, but I don't know the difference.

String pathToJar = System.getProperty("user.dir")+File.separator+"games"+File.separator+gameName;
    String requiredClassName = gameName;
    JarFile jarFile = new JarFile(pathToJar+".jar");
    Enumeration<JarEntry> e = jarFile.entries();

    URL[] urls = {new URL("jar:file:"+pathToJar+"!/")};
    URLClassLoader cl = URLClassLoader.newInstance(urls);

        while (e.hasMoreElements()) {
            JarEntry je = (JarEntry) e.nextElement();
            if(je.isDirectory() || !je.getName().endsWith(".class")){
                continue;
            }
            if (je.getName().contains(requiredClassName)){
                System.out.println(requiredClassName + " is found!"); 
            }
        String className = je.getName().substring(0,je.getName().length()-6);
        className = className.replace('/', '.');
        gameClass = cl.loadClass(className);
    }
    jarFile.close();

回答1:

This is what I tried, and I could make it work However it is advised to refer the 'javassist' jar for using ClassPool instead of ClassLoader

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;


public class LoadTheClassFromJar {
public static void main(String[] args) throws IOException, ClassNotFoundException {
    String pathToJar = "E:\\myjarfolder\\MyProject.jar";
    String requiredClassName = "Hungry";
    JarFile jarFile = new JarFile(pathToJar);
    Enumeration<JarEntry> e = jarFile.entries();

    URL[] urls = { new URL("jar:file:" + pathToJar+"!/") };
    URLClassLoader cl = URLClassLoader.newInstance(urls);

        while (e.hasMoreElements()) {
            JarEntry je = (JarEntry) e.nextElement();
            if(je.isDirectory() || !je.getName().endsWith(".class")){
                continue;
            }
            if (je.getName().contains(requiredClassName)){
                System.out.println(requiredClassName + " is found!"); 
            }
        // -6 because of .class
        String className = je.getName().substring(0,je.getName().length()-6);
        className = className.replace('/', '.');
        Class c = cl.loadClass(className);
        System.out.println(c.getName());
    }   
}
}

Output:

com.prash.MyClass
Hungry is found!
com.prash.Hungry

Note: I created my own jar file and dumped to that directory to test the program. I had a Hungry.java and MyClass.java in that project/jar file.



回答2:

Problem solved, but I don't really know why ... I use a File and toURI().toURL() to get the url

File jarFile = new File(System.getProperty("user.dir")+File.separator+"games"+File.separator+gameName+".jar");

    URLClassLoader cl = new URLClassLoader( new URL[]{jarFile.toURI().toURL()} );

    JarFile jar = new JarFile(jarFile.toString());
    Enumeration<JarEntry> e = jar.entries();
    while (e.hasMoreElements()) {
        JarEntry je = (JarEntry) e.nextElement();
        if(je.isDirectory() || !je.getName().endsWith(".class")){
            continue;
        }
        if (je.getName().contains(gameName)){
            String className = je.getName().substring(0,je.getName().length()-6);
            className = className.replace('/', '.');
            gameClass = cl.loadClass(className);
            System.out.println(gameName + " is loaded!");
        }
    }
    jar.close();
    cl.close();


标签: java class jar