简体   繁体   English

从jar加载一个类

[英]Load a class from a jar

I´m trying to load a class from a jar, I´m using a classLoader. 我正在尝试从jar加载一个类,我正在使用classLoader。

I have this parts of code for prepare the classLoader: 我有这部分代码用于准备classLoader:

private void loadClass(){

    try{
        JarFile jarFile = new JarFile( Path);
        Enumeration e = jarFile.entries();

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


    } catch (MalformedURLException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Now I load a class, and I try to get a new instance 现在我加载一个类,然后尝试获取一个新实例

....           
           loadClass();

           Class device = classLoader.loadClass( "org.myPackage.MyClass");

           MyMotherClass Device = ( MyMotherClass) device.newInstance();
...

MyClass extends of MyMotherClass, and when I do classLoader.loadClass( "org.myPackage.MyClass"), the MyMotherClass it is in the classLoader. MyClass扩展了MyMotherClass,当我执行classLoader.loadClass(“org.myPackage.MyClass”)时,MyMotherClass位于classLoader中。 At the moment, all right. 此刻,没事。

now, in device.newInstance(), I get a exception. 现在,在device.newInstance()中,我得到一个例外。 The problem is the other classes that are used by MyClass, are not in the classpath. 问题是MyClass使用的其他类不在类路径中。

What can i do? 我能做什么?

I have a another method that load all the needed classes in the classLoader, but does not work when I get the new instance. 我有另一个方法,在classLoader中加载所有需要的类,但是当我得到新实例时不起作用。 I can not change MyClass and the others 我不能改变MyClass和其他人

Here's some code I use to load a jar dynamically at run-time. 这是我用来在运行时动态加载jar的一些代码。 I exploit reflection to circumvent the fact that you ain't really supposed to do this (that is, modify the class path after the JVM has started). 我利用反射来规避你不应该这样做的事实 (也就是说,在JVM启动后修改类路径)。 Just change my.proprietary.exception to something sensible. 只需将my.proprietary.exception更改为合理的内容即可。

    /*
     * Adds the supplied library to java.class.path.
     * This is benign if the library is already loaded.
     */
    public static synchronized void loadLibrary(java.io.File jar) throws my.proprietary.exception
    {
        try {
            /*We are using reflection here to circumvent encapsulation; addURL is not public*/
            java.net.URLClassLoader loader = (java.net.URLClassLoader)ClassLoader.getSystemClassLoader();
            java.net.URL url = jar.toURI().toURL();
            /*Disallow if already loaded*/
            for (java.net.URL it : java.util.Arrays.asList(loader.getURLs())){
                if (it.equals(url)){
                    return;
                }
            }
            java.lang.reflect.Method method = java.net.URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{java.net.URL.class});
            method.setAccessible(true); /*promote the method to public access*/
            method.invoke(loader, new Object[]{url});
        } catch (final NoSuchMethodException | 
            java.lang.IllegalAccessException | 
            java.net.MalformedURLException | 
            java.lang.reflect.InvocationTargetException e){
            throw new my.proprietary.exception(e.getMessage());
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM