aspose file tools*
The moose likes Java in General and the fly likes Runtime.getRuntime( ).exec( command ); Win vs Unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime.getRuntime( ).exec( command ); Win vs Unix" Watch "Runtime.getRuntime( ).exec( command ); Win vs Unix" New topic
Author

Runtime.getRuntime( ).exec( command ); Win vs Unix

Jonathan Bagshaw
Greenhorn

Joined: May 05, 2006
Posts: 14
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?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: May 05, 2006
Posts: 14
<3 Keith.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jonathan Bagshaw
Greenhorn

Joined: May 05, 2006
Posts: 14
Yes.. dir and ls were simply for prototyping purposes.
Brian Stinar
Greenhorn

Joined: Mar 16, 2007
Posts: 3
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.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41524
    
  53
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


Ping & DNS - my free Android networking tools app
Brian Stinar
Greenhorn

Joined: Mar 16, 2007
Posts: 3
Thanks a lot!!

-Brian J. Stinar-
 
Consider Paul's rocket mass heater.
 
subject: Runtime.getRuntime( ).exec( command ); Win vs Unix