Java - NoClassDefFoundError

I need to compile an external Java file (say a.java). This is the code I wrote for it.

(String path contains java file path and class)

    command[0] = "javac";
    command[1] = path+"a.java";
    p = Runtime.getRuntime().exec(command);        

      

The above code works fine. But below code

    command[0] = "java";
    command[1] = "a";
    command[2] = "-cp";
    command[3] = "."+path+"a";
    p = Runtime.getRuntime().exec(command);        
    stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
    stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    while ((temp = stdInput.readLine()) != null) result += "\n" + temp;
    while ((temp = stdError.readLine()) != null) result += "\n" + temp;

      

Throws the following error:

java.lang.NoClassDefFoundError: a
Exception in thread "main" 

      

Can anyone please explain the problem with this code. Thank!

+3


source to share


1 answer


You give the class name first and then the arguments for the classpath. The class name is always the last thing needed before the program-specific arguments. So in your case, the classpath part will not be treated as an option - it will be treated as two arguments ( -cp

and a path) to the Java program itself.

So instead of:

java a -cp (whatever)

      



Do you want to

java -cp (whatever) a

      

+4


source







All Articles