Rooster
Rooster

Reputation: 1277

Runtime.exec() gives Error: Could not find or load main class

'Street.class' in my Eclipse project is under \bin in package trafficcircle. The error below is from stderror of the created process; I thought Runtime.exec would complain first if it wasn't found...what's up with this?

Code that runs 'Street' process:

    Process process = runtime.exec("java -classpath \\bin trafficcircle.Street 1 2");

Where 'Street' is:

public class Street {

/**
 * @param args
 * 0 - Simulation run time
 * 1 - Flow time interval
 */
public static void main(String[] args) {
    System.out.println(args[0]);
    System.out.println(args[1]);
    System.out.flush();
}
}

Process prints out:

Error: Could not find or load main class trafficcircle.Street

Process exitValue: 1

And yes, this works on cmd line:

C:\Users\Brent>java -classpath "D:\Java Programs\IPCTrafficCircle\bin" trafficcircle.Street 1 2

Upvotes: 2

Views: 4142

Answers (2)

wannik
wannik

Reputation: 12696

This code gives the expected result when run in the folder /bin by typing the command line java Test.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Test {
    public static void main(String[] args) throws Exception {
        Process process = Runtime.getRuntime().exec(
                "java trafficcircle.Street 1 2");

        BufferedReader br = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}

However, it doesn't give any result when run in Eclipse. To get the result, I have to set the class path.

"java -cp /Users/wannik/Java/Workspace/MyProject/bin trafficcircle.Street 1 2");

Upvotes: 2

Brian Roach
Brian Roach

Reputation: 76898

Runtime.exec() would complain if java wasn't found, which is the process you are running. The message you are reading is coming from that process' output.

Do you notice a difference between what you're exec'ing and what you say works on the command line? That's the problem.

The java JVM you are exec'ing needs to be able to find the class you want it to run. The classpath you are giving it (\bin) isn't correct.

Upvotes: 1

Related Questions