• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime.exec()

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The pitfalls of Runtime.exec are a (very) frequently asked question - so the Java Intermediate FAQ has something to say on the subject.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EaseUp...

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.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic