wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Runtime.exec() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime.exec()" Watch "Runtime.exec()" New topic


Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Can anyone help me out that how to use exec() method of Runtime class Because everytime I use it I got an execption.

java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:66)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:566)
at java.lang.Runtime.exec(Runtime.java:428)
at java.lang.Runtime.exec(Runtime.java:364)
at java.lang.Runtime.exec(Runtime.java:326)
at Test.main(Test.java:9)

pls help me out my code was this as following

The Best way to predict your future is to create it - Every great individual common man
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41101
The pitfalls of Runtime.exec are a (very) frequently asked question - so the Java Intermediate FAQ has something to say on the subject.

Ping & DNS - my free Android networking tools app
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In this particular case you probably had to be programming on DOS machines before Windows to remember that "dir" is not a program, but a built-in command in the command shell. Open a command window and enter "command /?" and "cmd /?" for an introduction to the weirdness that is Windows. Sometimes you have to run "cmd xxxx" as the command with exec(), and sometimes "command xxxx". Since these commands were written to work at a console window and you don't get one with exec() you'll want to look into reading the stdout and errout streams from the Process that is returned by exec.

If you're in Java5 or later, look into ProcessBuilder as a friendlier alternative to exec(). It won't help with this problem, but it's a nice way to build commands.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Well I am still confused because I want to run some dos commands on my OS(Windows XP). And I have Jdk 1.4. SO please suggest me what to do. or How can i Run dos commands from my Java Program. Is there any alternative method to run Dos Commands. Pls suggest me as soon as possible.
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364

Ulf posted a link to the FAQ. I suggest you read it. In that FAQ it uses the exact example of what you are trying to do -- run dir command from java. Then it has a code example of how to do it.
Paul Clapham

Joined: Oct 14, 2005
Posts: 18541

And hopefully, "dir" was just an example and you really want to run some other DOS command. Because it's much more practical to use the listFiles() method of the java.io.File class to get a list of the files in a directory.
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Actually "dir" was just an example but I want to run all commands over Dos that's why i ask this question. I just wanna to know how to run the command in Dos prompt by Java or how to run a win16 Exe on dos command through Java.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Running an EXE is easy. That's what exec() does best. Running DOS commands means you have to run CMD.EXE or COMMAND.EXE and pass the command as arguments. Again, "cmd /?" or "command /?" will reveal all the magic.

One bit of bad news is you have to know what's a built-in command and what is an EXE. If the user enters an arbitrary string for you to execute it might be safest to always use cmd (or is it command?) to run everything.

Oh, and I don't think 16-bit programs will be any problem. I still run my old Turbo Pascal exe's that I'm pretty sure are 16-bit.
[ February 16, 2006: Message edited by: Stan James ]
It is sorta covered in the JavaRanch Style Guide.
subject: Runtime.exec()
Similar Threads
running OS commands in Java
passing String to the command line
Pesky Runtime method
Running DOS commands
exec() in win2000