• 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.getRuntime( ).exec( command ); Win vs Unix

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code segment fails in windows, but works under unix:

(note: command = "ls" under unix)

The stack trace is :
C:\CVS_ROOT>java -classpath Carpenter.jar carpenter.Carpenter
java.io.IOException: CreateProcess: dir error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at carpenter.Carpenter.main(Carpenter.java:17)

I'm developing under Windows.. Can anyone enlighten me as to how I can use this code in a Windows env?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Windows, dir is not a standalone program. It is built into the command shell.

So on Windows you would do something like this.

.

You might also look at ProcessBuilder.
 
Jonathan Bagshaw
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<3 Keith.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Presumably, "dir" and "ls" were just example commands picked for prototyping purposes, yes? There's little/no reason to invoke them from Java for any real application, is there?

For "dir", the trick of running it via "cmd" is fine. But most things you would type into a Windows Command Prompt are separate programs and do not need a "cmd". It would be inefficient to use "cmd" for most things.
 
Jonathan Bagshaw
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.. dir and ls were simply for prototyping purposes.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have been looking for places to help me understand the format my String[] cmd must be in for different operating systems. I need to be able to run (primarily) on Windows XP and on Linux. Do you guys know of any good links to help me get a handle on this?

Thanks a lot!!

-Brian J. Stinar-

Originally posted by Keith Lynn:
In Windows, dir is not a standalone program. It is built into the command shell.

So on Windows you would do something like this.

.

You might also look at ProcessBuilder.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian, welcome to JavaRanch.

There's an all-time favorite article on the pitfalls of Runtime,exec: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps_p.html
 
Brian Stinar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!!

-Brian J. Stinar-
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic