0

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?

3
  • cd does not exist outside the command prompt, and you're not using the command prompt in your code. Commented Jun 5, 2021 at 10:49
  • The string literal has unescaped `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 executable cmd, sh` or similar depending upon platform.) Commented Jun 5, 2021 at 10:50
  • @TomHawtin-tackline sorry for the not compilable string, I am building the string using variables, but I copy/pasted it wrong. I have edited the string to how it should work. I need to run this program on linux and windows. How can I use the command prompt? Commented Jun 5, 2021 at 10:55

2 Answers 2

6

You can't run cd as a command. You can instead set the working directory when you exec your command.

Process compileSimulatorProcess = Runtime
    .getRuntime()
    .exec(
        "mvn package", 
        null, 
        new File("C:/Users/Alexander/Projects/Simulator"));

Also might need import java.io.File

See docs for more info about this overload of exec.

Sign up to request clarification or add additional context in comments.

2 Comments

java.io.IOException: Cannot run program "mvn" (in directory "C:\Users\Alexander\Projects\Simulator"): CreateProcess error=2, The system cannot find the file specified, why can't I run mvn commands?
mvn may not be in your Path. To check, in a cmd, run where mvn and use the full path. If it says not found, make sure mvn is installed.
0

Also here may be the problem with run actual .cmd (not .exe) files from ProcessBuilder.

For .cmd files add cmd.exe /c at the beginning of your command string.

Thanks jeroen-de-schutter and mahozad for this https://stackoverflow.com/a/21281570/15337095

2 Comments

If the answer to another question is also appropriate for this question, then this question is a duplicate. Hence, rather than copying the other answer, you should mark this question as a duplicate.
i think it's not duplicate, these are different questions, but touch same topic

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.