Runtime.exec () gives an error: could not find or load the main class

'Street.class' in my Eclipse project is located in \ bin in a workaround for packages. The following is the error from the stderror of the created process; I thought Runtime.exec would have come first if it hadn’t been found ... what about this?

Code that launches the Street process:

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

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(); } } 

The process is printing:

Error: could not find or load the main class trafficcircle.Street

Process exitValue: 1

And yes, this works on the cmd line:

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

+2
source share
2 answers

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

 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, when launched in Eclipse it does not give any result. To get the result, I have to set the class path.

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

Runtime.exec() will complain if java was not found, this is the process you are running. The message you are reading comes from the output of this process.

Have you noticed the difference between what you do and what you say works on the command line? This is problem.

The jVM java that you are running should be able to find the class you want to run. The path to the class you give it ( \bin ) is incorrect.

+1
source

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


All Articles