Code that runs 4 'Street' processes:
for (int i=0; i < NUM_STREETS; i++) {
Process process = runtime.exec("java -classpath \\bin trafficcircle.Street 1 2");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null && !line.isEmpty()) {
System.out.println(line);
System.out.flush();
}
InputStream es = process.getErrorStream();
InputStreamReader esr = new InputStreamReader(es);
BufferedReader br2 = new BufferedReader(esr);
while ((line = br2.readLine()) != null && !line.isEmpty()) {
System.out.println(line);
System.out.flush();
}
int exitVal = process.waitFor();
System.out.println("Process exitValue: " + exitVal);
}
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();
}
}
Prints out:
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
Error: Could not find or load main class trafficcircle.Street
Process exitValue: 1
'Street.class' in my Eclipse project is under \\bin in package trafficcircle. I thought Runtime.exec would complain first if it wasn't found...what's up with this?
I assume you are getting an error which you are discarding. Try using ProcessBuilder.redirectErrorStream(true);
When you try to run a command it is not run in a shell, and may be getting an error which you don't see on the command line. I would explicitly use
"java","-classpath","bin","trafficcircle.Street","1","2"`
and make sure you are getting any error messages.
another option is to use a shell like
"/bin/bash", "-c", "java -classpath bin trafficcircle.Street 1 2"
使用./bin(带点)来使用相对路径。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.