I have a problem. I am trying to execute the following command in java:
cd C:\Users\Alexander\Projects/Simulator && mvn package
In the CMD command line, this command does get executed correctly, but when I try to run it in java using the following code:
try {
String compileSimulatorCommand = "cd C:/Users/Alexander/Projects/Simulator && mvn package";
Process compileSimulatorProcess = Runtime.getRuntime().exec(compileSimulatorCommand);
compileSimulatorProcess.waitFor();
}
catch (Exception e) {
e.printStackTrace();
}
But this returns in the following error:
java.io.IOException: Cannot run program "cd": CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1142)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
at java.base/java.lang.Runtime.exec(Runtime.java:591)
at java.base/java.lang.Runtime.exec(Runtime.java:415)
at java.base/java.lang.Runtime.exec(Runtime.java:312)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:305)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.base/java.lang.ProcessImpl.create(Native Method)
at java.base/java.lang.ProcessImpl.<init>(ProcessImpl.java:483)
at java.base/java.lang.ProcessImpl.start(ProcessImpl.java:158)
at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1109)
How can I fix this?
cddoes not exist outside the command prompt, and you're not using the command prompt in your code.`s and does not compile. (Also you seem to be trying for a shell command rather than executing an executable. So you should use the executablecmd,sh` or similar depending upon platform.)