This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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-
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic