jeudi 16 septembre 2021

Reflective Java exception when invoke method is used

I just started using reflective programming in Java, there is not much going on really, I am simply trying to invoke the class itself. Keep in mind that the code is all inside a class called TestBench and the package is lab01. The exception appears after the line before the last catch

m.invoke("lab01.TestBench", args);

I am confident that the issue is at the first argument.

public static void main(String[] ars)  
{
    
    try {
        testAlgorithm("Algorithms", "linear", 50);
        
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.err.println(e);
    }
    
      
}
 
{

    try
    {
        String name = "lab01.TestBench";
        Class cls = Class.forName("TestBench");
        
        Class partypes[] = new Class[4];
        partypes[0] = String.class;
        partypes[1] = Integer.TYPE;
        partypes[2] = Integer.TYPE;
        partypes[3] = Integer.TYPE;
        Method m = cls.getMethod(name, partypes);
        
        //TestBench ts = new TestBench();
        //String str = java.lang.String("TestBench");
        
        //Algorithms a = new Algorithms();
        Object args[] = new Object[4];
        args[0] = new String(methodName + ".txt");
        args[1] = Integer.valueOf(3);
        args[2] = Integer.valueOf(1);
        args[3] = n;
        
        m.invoke("lab01.TestBench", args);
        
    }
    catch (Throwable e)
    {
        System.err.println(e);
    }
}




Aucun commentaire:

Enregistrer un commentaire