动态加载Jar并实现加载类的对象 [英] Dynamically loading Jar and instanciate an Object of a loaded Class

查看:155
本文介绍了动态加载Jar并实现加载类的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是类加载器的代码:

  public class ClassLoad {

public static void main(String [] args){

String filePath = new String(C: /Users/Mehdi/Desktop/JavaClassLoader/jarred.jar);

URL myJarFile = null;
try {
myJarFile = new URL(file://+ filePath);
} catch(MalformedURLException e1){
System.out.println(1);
e1.printStackTrace();
}

URLClassLoader cl = URLClassLoader.newInstance(new URL [] {m​​yJarFile});

Class Jarred = null;
try {
Jarred = cl.loadClass(com.jarred.exp.Jarred);
} catch(ClassNotFoundException e){
System.out.println(2);
e.printStackTrace();
}

方法simpleWrite = null;
try {
simpleWrite = Jarred.getMethod(simpleWrite,new Class [] {String.class});
} catch(SecurityException e){
System.out.println(3);
e.printStackTrace();
} catch(NoSuchMethodException e){
System.out.println(4);
e.printStackTrace();
}

对象JarredObj = null;
try {
JarredObj = Jarred.newInstance();
} catch(InstantiationException e){
System.out.println(5);
e.printStackTrace();
} catch(IllegalAccessException e){
System.out.println(6);
e.printStackTrace();
}

try {
Object response = simpleWrite.invoke(JarredObj,\Hello Mehdi!It works hamdoulillah:D);
} catch(IllegalArgumentException e){
e.printStackTrace();
} catch(IllegalAccessException e){
e.printStackTrace();
} catch(InvocationTargetException e){
e.printStackTrace();
}
}
}

和包含的类进入Jar:

  package com.jarred.exp; 

public class Jarred {

public void simpleWrite(String str){
System.out.println(str);
}

}



它给了我:

  2 
java.lang.ClassNotFoundException:com.jarred.exp.Jarred
在java.net.URLClassLoader $ 1.run(未知源)
在java.security.AccessController.doPrivileged(本机方法)
在java.net.URLClassLoader.findClass(未知来源)
在java.lang.ClassLoader.loadClass(未知源)
在java.net.FactoryURLClassLoader.loadClass(未知源)
在java.lang.ClassLoader.loadClass(未知来源)
在ClassLoad。 main(ClassLoad.java:25)
线程main中的异常java.lang.NullPointerException在ClassLoad.main(ClassLoad.java:32)

/ pre>

你有什么想法吗?谢谢。

解决方案

看起来您的文件网址无效。



Windows中的文件URI


对于本地Windows文件路径



C:\Documents and Settings\davris\FileSchemeURIs.doc



Windows中相应的有效文件URI :



file:/// C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc


其中显示冒号后需要 ,而您在

中计算的URL

  String filePath = new String(C:/Users/Mehdi/Desktop/JavaClassLoader/jarred.jar); 

URL myJarFile = null;
try {
myJarFile = new URL(file://+ filePath);

文件之后只有两个斜杠 。可能

  myJarFile = new URL(file://+ filePath); 

应该是

 code> myJarFile = new URL(file:///+ filePath); 

或者你可以使用 java.io.File.toURI 因此

 文件myJarFile =新文件(C:\\Users\\Mehdi\\Desktop \\JavaClassLoader\\jarred.jar); 
if(!myJarFile.isFile()){
throw new FileNotFoundException(Missing required JAR:+ myJarFile.toString());
}
URL myJarUrl = myJarFile.toURI()。toURL();

具有适当的异常处理。


I try to load dynamically a jar into my Java project.

Here's the class loader's code :

public class ClassLoad {

public static void main(String[] args) {

    String filePath = new String("C:/Users/Mehdi/Desktop/JavaClassLoader/jarred.jar");

    URL myJarFile = null;
    try {
        myJarFile = new URL("file://"+filePath);
    } catch (MalformedURLException e1) {
        System.out.println("1");
        e1.printStackTrace();
    }

    URLClassLoader cl = URLClassLoader.newInstance(new URL[]{myJarFile});

    Class Jarred = null;
    try {
        Jarred = cl.loadClass("com.jarred.exp.Jarred");
    } catch (ClassNotFoundException e) {
        System.out.println("2");
        e.printStackTrace();
    }

    Method simpleWrite = null;
    try {
        simpleWrite = Jarred.getMethod("simpleWrite", new Class[] {String.class});
    } catch (SecurityException e) {
        System.out.println("3");
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        System.out.println("4");
        e.printStackTrace();
    }

    Object JarredObj = null;
    try {
        JarredObj = Jarred.newInstance();
    } catch (InstantiationException e) {
        System.out.println("5");
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        System.out.println("6");
        e.printStackTrace();
    }

    try {
        Object response = simpleWrite.invoke(JarredObj, "\nHello Mehdi ! It works hamdoulillah :D");
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
}
}

and the Class which is included into the Jar :

package com.jarred.exp;

public class Jarred {

public void simpleWrite(String str) {
    System.out.println(str);
}

}

It gives me :

2
java.lang.ClassNotFoundException: com.jarred.exp.Jarred
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at ClassLoad.main(ClassLoad.java:25)
Exception in thread "main" java.lang.NullPointerException
at ClassLoad.main(ClassLoad.java:32)

Do you have any idea about this ? Thank you.

解决方案

It looks like your file URL is invalid.

"File URIs in Windows" says

For the local Windows file path

C:\Documents and Settings\davris\FileSchemeURIs.doc

The corresponding valid file URI in Windows is:

file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc

which shows that three slashes are needed after the colon, but the URL you are computing in

String filePath = new String("C:/Users/Mehdi/Desktop/JavaClassLoader/jarred.jar");

URL myJarFile = null;
try {
    myJarFile = new URL("file://"+filePath);

has only two slashes after the file:. Perhaps

    myJarFile = new URL("file://"+filePath);

should be

    myJarFile = new URL("file:///"+filePath);

or alternatively you could use java.io.File.toURI thus

File myJarFile = new File("C:\\Users\\Mehdi\\Desktop\\JavaClassLoader\\jarred.jar");
if (!myJarFile.isFile()) {
  throw new FileNotFoundException("Missing required JAR: " + myJarFile.toString());
}
URL myJarUrl = myJarFile.toURI().toURL();

with appropriate exception handling.

这篇关于动态加载Jar并实现加载类的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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