动态加载Jar并实例化已加载类的Object

时间:2013-04-19 12:28:07

标签: java web-services dynamic client classloader

我尝试将jar动态加载到我的Java项目中。

这是类加载器的代码:

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

以及包含在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
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)

你对此有任何想法吗?谢谢。

1 个答案:

答案 0 :(得分:2)

您的文件网址似乎无效。

"File URIs in Windows"

  

对于本地Windows文件路径

     

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

     

Windows中相应的有效文件URI是:

     

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

表示冒号后需要三个斜杠,但是你要计算的网址

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

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

file:之后只有两个斜杠。也许

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

应该是

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

或者您也可以使用java.io.File.toURI

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

进行适当的异常处理。