简体   繁体   中英

Java loading an external class in runtime

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!

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)

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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