Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.IOException: Cannot run program "set": CreateProcess error=2, The system cannot find the file specified [duplicate]

I am trying to run set command from eclipse, but i am getting the below exception.

java.io.IOException: Cannot run program "set": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
    at java.lang.Runtime.exec(Runtime.java:620)
    at java.lang.Runtime.exec(Runtime.java:450)
    at java.lang.Runtime.exec(Runtime.java:347)

Here is my piece of code:

String command = "set Path=C:/Program Files/Java/jdk1.6.0_21/bin";
Process p = Runtime.getRuntime().exec(command);
like image 464
HimaaS Avatar asked Dec 28 '25 13:12

HimaaS


1 Answers

The program fails because set is not an executable but a command inside the command processor cmd.exe.

To invoke it use

String command = "cmd.exe /c set path=C:/Program Files/Java/jdk1.6.0_21/bin";
Process p = Runtime.getRuntime().exec(command);

But be aware of the pitfalls of setting environment variables, see How to set an environment variable in Java using exec? as mentionend in the comments by @Berger

like image 162
wero Avatar answered Dec 31 '25 03:12

wero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!