File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jonathan Bagshaw
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<3 Keith.
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.. dir and ls were simply for prototyping purposes.
 
Brian Stinar
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!!

-Brian J. Stinar-
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic