使用他的名字在jar中加载类 [英] Load a class in a jar just with his name

查看:174
本文介绍了使用他的名字在jar中加载类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在jar中加载类,但未成功。

  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));
枚举< 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();
}



我认为问题来自 URLClassLoader 可能不在正确的地方。



使用OP的更新编辑问题[



我的@spiderman版本仍然有错误。
我确切的说gameClass类型是类<?> ,不是,但我不知道差异。



/ p>

  String pathToJar = System.getProperty(user.dir)+ File.separator +games+ File.separator + gameName; 
String requiredClassName = gameName;
JarFile jarFile = new JarFile(pathToJar +。jar);
枚举< 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();问题解决了,但我真的不知道为什么要解决这个问题。

...
我使用一个文件和toURI()。toURL()获取url

 文件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());
枚举< 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 6);
className = className.replace('/','。');
gameClass = cl.loadClass(className);
System.out.println(gameName +is loaded!);
}
}
jar.close();
cl.close();


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();

解决方案

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();

这篇关于使用他的名字在jar中加载类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆