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
Joined: Mar 22, 2005
The pitfalls of Runtime.exec are a (very) frequently asked question - so the Java Intermediate FAQ has something to say on the subject.
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
Joined: Dec 27, 2005
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.
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.
Joined: Dec 27, 2005
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.
Joined: Jan 29, 2003
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 ]