尝试在Java运行时从jar文件加载类时发生ClassNotFoundException

时间:2018-10-14 09:17:16

标签: java class classloader urlclassloader dynamic-class-loaders

我正在尝试从jar文件中的类动态执行函数。我有jar文件的位置,类的名称以及其中的函数作为字符串。

我看了这些问题,但没有一个答案对我有用:

How should I load Jars dynamically at runtime?

ClassNotFoundException while trying to load class from external JAR at runtime

How to load a jar file at runtime

Dynamically load a jar

Load jar dynamically at runtime?

这是到目前为止我得到的: 在主程序中:

    public class Main
    {

        public static void main(String[] args)
        {
            File file = new File("E:\\DeSKtop\\hw.jar");   
            String lcStr = "Main1";   
            URL jarfile;
            try {
                jarfile = new URL("jar", "","file:" + file.getAbsolutePath()+"!/");
                URLClassLoader cl = URLClassLoader.newInstance(new URL[] {jarfile });   
                Class loadedClass = cl.loadClass(lcStr);
                Method method = loadedClass.getDeclaredMethod("returnHW");
                Object instance = loadedClass.newInstance();
                Object result = method.invoke(instance);
                //System.out.println(method.invoke());
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
            //System.out.println(loadLibrary(myJar));
            catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

在我要加载的jar文件中:

public class Main1 {
    public static void main(String[] args)
    {

    }
    public static String returnHW()
    {
        System.out.println("HlloWOrld");;
        return "Hello Wrld!";
    }

}

当我尝试运行主程序时,我得到了:

java.lang.ClassNotFoundException: Main1
    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 Main.main(Main.java:22)

你能告诉我我在做什么错吗?预先谢谢你。

1 个答案:

答案 0 :(得分:1)

解决方案是您还必须声明软件包。

如果您在存储类名的地方更新了变量lcStr,以包含该软件包,则它应该起作用。

示例:

        String lcStr = "com.company.Main1";