user1511360
user1511360

Reputation: 15

ClassNotFoundException while using Class.forname

I am writing a generic interpreter. It contains primitive processes (such as method calling, return from method, control statements, etc.), that are run by a translator that reads the source code, written in any language, and activates the primitives. The core of this translator is constructed from a strings array that contains the names of the methods that implemets the processes and dynamic activation of the method.

The following code is used to invoke the method pointed to by the index procIndex. It is based on an example I found, and is the same as other examples:

try {
    Class<?> c = Class.forName("FinalTestDecoder");
    Method  commandExe = 
         c.getDeclaredMethod (commandsTable[commandIndex][methodName], (Class<?>[])null);
    commandExe.invoke (commandExe, (Object []) null);
} catch (IllegalAccessException| IllegalArgumentException| 
       InvocationTargetException| EmptyStackException | 
        ClassNotFoundException | NoSuchMethodException | SecurityException  e) { 
  handleErr(cmdMethodNotFound, "Command: "+ commandsTable[commandIndex][programCommand]); 
} finally {
   found = true; 
}   // Cause the loop to terminate

The handleErr method is handling the error condition and gets decimal error code and a string. The error handler can be invoked by any of the exceptions.

The problem is in the line Class<?> c = Class.forName("FinalTestDecoder");, that throws the ClassNotFoundException. To overcome this I made two experiments: originally, the invoked methods were in the same class as the invoking code; n the second I created an embedde class that contained the invoked methods. The result was the same.

Well, I spent hours on this issue and ran out of ideas. any help will be thankfully welcomed

Upvotes: 2

Views: 894

Answers (1)

Rahul
Rahul

Reputation: 45060

You must use the full qualified class name, which is required for the method forName. Something like this:-

Class.forName("com.test.class.file.diretory.FinalTestDecoder");

Upvotes: 7

Related Questions