Java loading an external class in runtime

拥有回忆 提交于 2019-12-11 03:28:53

问题


I have yet another issue which the answer is eluding me. I wish to take a class from an external jar at runtime and grab a method from it and pass it a parameter. My code below currently opens the jar and grabs the class and runs the method but when I try and pass it a parameter, the method runs but I get an InvocationTargetException. Any ideas?

Here is my code:

    String path = "test.jar";
    URL[] classes = {new File(path).toURI().toURL()};
    URLClassLoader child = new URLClassLoader (classes, this.getClass().getClassLoader());
    try {
        Class classToLoad = Class.forName("testClass", true, child);
        Method method = classToLoad.getDeclaredMethod ("testMethod", String.class);
        Object instance = classToLoad.newInstance();
        Object result = method.invoke(instance, new String("Test from method!"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }

And here is the error it throws:

Test from method!
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at Load.loadJar(LoadTerrem.java:33)
    at Load.<init>(LoadTerrem.java:18)
    at Load.main(LoadTerrem.java:13)
Caused by: java.lang.NullPointerException
    at MenuSingleplayer.LoadWorlds(MenuSingleplayer.java:210)
    at MenuSingleplayer.setup(MenuSingleplayer.java:89)
    at M0.LoadGame(M0.java:76)
    ... 7 more

As you can see, the method executes, printing out the string passed to it but then throws an error on the line:

Object result = method.invoke(instance, new String("Test from LoadTerrem!"));

Any ideas? Thanks!


回答1:


An InvocationTargetException is thrown when the invoked method (testMethod in your case) throws an exception. From the docs:

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor.

Judging from your stack trace (the "caused by"-part to be precise) it looks like testMethod causes MenuSingleplayer.LoadWorlds to be called, which raises a NullPointerException. This NullPointerException propagates up until it reaches the reflective call, at which point the it is wrapped in an InvocationTargetException.




回答2:


The problem is in the MenuSingleplayer.LoadWorlds method. It says a NPE happened there. There is nothing to do with reflection. It's OK:

Caused by: java.lang.NullPointerException
at MenuSingleplayer.LoadWorlds(MenuSingleplayer.java:210)


来源:https://stackoverflow.com/questions/9898102/java-loading-an-external-class-in-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!