Runtime.exec () no output

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 is the "Street":

 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 located in \ bin in a workaround for packages. I thought Runtime.exec would have come first if it hadn’t been found ... what about this?

+4
source share
2 answers

I assume that you are getting the error you are throwing. Try using ProcessBuilder.redirectErrorStream(true);

When you try to run a command, it does not start in the shell and an error may appear that you do not see on the command line. I would obviously use

 "java","-classpath","bin","trafficcircle.Street","1","2"` 

and make sure you get error messages.

another option is to use a shell like

 "/bin/bash", "-c", "java -classpath bin trafficcircle.Street 1 2" 
+2
source

Use. / Bin (with a dot) to use relative paths.

0
source

Source: https://habr.com/ru/post/1380885/


All Articles