This question is being asked everywhere on Google but I'm still having trouble with it. Here is what I'm trying to do. So like my title states, I'm getting an 'object is not an instance of declaring class' error. Any ideas? Thanks!
Main.java
Class<?> base = Class.forName("server.functions.TestFunction"); Method serverMethod = base.getMethod("execute", HashMap.class); serverMethod.invoke(base, new HashMap<String, String>());
TestFunction.java
package server.functions; import java.util.HashMap; import java.util.Map; import server.*; public class TestFunction extends ServerBase { public String execute(HashMap<String, String> params) { return "Test function successfully called"; } }
You're invoking the method with the class, but you need an instance of it. Try this:
serverMethod.invoke(base.newInstance(), new HashMap<String, String>());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With