Problem invoking method of a newly loaded class

0

I made a code that dynamically compiles a java file, and through the .class generated with load this class, create an instance and invoke its methods using the getDeclaredMethod() method. So far so good. But now I'm encountering an error when I try to do this same process in a test class. It compiles and loads, but I can not access the methods. The class name is Teste and inside it I have the test() method. When I try to invoke method teste() the error that is returned to me says: java.lang.NoSuchMethodException: Teste.test() Does anyone know why this is happening with this class? Here are the codes:

Class that compiles and loads java files:

public class ReflectApp{

    public static void main(String[] args) throws IOException, NoSuchMethodException {
        String arquivo = "/C:/teste/Teste.java";

        PrintWriter saida = new PrintWriter(new FileWriter("logCompilacao.txt"));
        int resultadoCompilacao1 = com.sun.tools.javac.Main.compile(new String[]{arquivo},saida);
        System.out.println("Show: " + resultadoCompilacao1);


    try{

        URL classUrl;
        classUrl = new URL("file:///C:/teste/");
        URL[] classUrls = { classUrl };
        URLClassLoader ucl = new URLClassLoader(classUrls);
        Class c = ucl.loadClass("Teste");

        Object obj = c.newInstance();   
        Method method = c.getDeclaredMethod("test", null);
        System.out.println(method.invoke(obj, null));


    }catch(Exception ex){
        ex.printStackTrace();
    }
   }
}

Class where I want to invoke methods:

public class Teste {

    @Test
    public void test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IOException {

        ExternalClassBuilder my_class = new ExternalClassBuilder();
        Class c = my_class.run("/C:/teste/", "Operation");
        Object obj = c.newInstance();   

        Method method = c.getDeclaredMethod("sum", int.class, int.class);
        System.out.println(method.invoke(obj, 1, 2));
        int numero = (int) method.invoke(obj, 1, 2);
        if(numero == 3){
            System.out.println("Método correto");
        }else{
            System.err.println("Método SUM incorreto");
            fail("Valor não corresponde ao esperado");
        }
        Method method2 = c.getDeclaredMethod("hello", null);
        System.out.println(method2.invoke(obj));    

    }

}

Regardless of what the test() method does, the problem I see is that the method is not even found.

    
asked by anonymous 07.04.2016 / 16:04

1 answer

-1

To call a method directly from the class so Teste.test() you need to change the line:

public void test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IOException {

To:

public static void test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IOException {

The difference is that I've added 'static' which will make your method static, so it can be 'seen' in needing to test the 'Test' class.

If you do not want to make the above change, you need to instantiate the class to call the method, which might look like this:

Teste objTeste = new Teste();
objTeste.test();

Or so:

(new Teste()).test();

Try the feedback and pass feedback!

    
07.04.2016 / 16:12