File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Exception - Running command line from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception - Running command line from Java " Watch "Exception - Running command line from Java " New topic

Exception - Running command line from Java

Raja Sathci

Joined: Sep 16, 2005
Posts: 10

Below is a code snippet i am using in my program,

String cmd = "set itr="+count;
Process p = Runtime.getRuntime().exec(cmd);

When i run the program i get the following exception,

set itr=5 CreateProcess: set itr=5 error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(
at java.lang.ProcessImpl.start(
at java.lang.ProcessBuilder.start(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at java.lang.Runtime.exec(
at ReadFile.main(

Can any one please let me know how to overcome this.
Basically i am trying to set a environment variable from java.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14487

The command "set" is not an executable program, it's a built-in command of the command prompt window or shell. So you cannot use it to set an environment variable like this.

What operating system are you using? As far as I know there is no (portable) way in Java to set system-wide environment variables. Note that if you start a program with Runtime.exec(), it will be started in its own environment, and when the program is finished that environment disappears again, and the environment variables that you set in that environment will be gone too.

Have a look at class ProcessBuilder for running executables, in particular the environment() method.

What are you trying to achieve with this?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
wood burning stoves
subject: Exception - Running command line from Java