aspose file tools*
The moose likes Beginning Java and the fly likes How to Run DOS Commands( ipconfig /All >> a.txt) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to Run DOS Commands( ipconfig /All >> a.txt)" Watch "How to Run DOS Commands( ipconfig /All >> a.txt)" New topic
Author

How to Run DOS Commands( ipconfig /All >> a.txt)

Arun thoppegowda shivanna
Greenhorn

Joined: Aug 16, 2004
Posts: 6
I want run command (ipconfig /All >> a.txt) thru Java. I Know
exec method of Runtime class which runs as a seperate process, but it works for simple command like "edit" "command" but i don't how to pass the arguments.
[ August 16, 2004: Message edited by: Arun thoppegowda shivanna ]
Rovas Kram
Ranch Hand

Joined: Aug 08, 2003
Posts: 135
The 'exec' method is overloaded to include 'exec(String[])'. Call this method passing your arguments as such: {"ipconfig", "/all", ">", "a.txt"}.
Note that the Operating System may not let you run certain commands this way.
Petr Blahos
Ranch Hand

Joined: Apr 28, 2004
Posts: 131
Originally posted by Rovas Kram:
The 'exec' method is overloaded to include 'exec(String[])'. Call this method passing your arguments as such: {"ipconfig", "/all", ">", "a.txt"}.
Note that the Operating System may not let you run certain commands this way.


No. I would be surprised if this ever worked because java wouldn't
start a new shell for every exec. (Read: you can't do redirection.)
Besides it isn't portable. Please read thread:

http://www.coderanch.com/t/374116/java/java/Problems-Runtime-exec

P.


Get a better web browser:<br /><a href="http://www.mozilla.org/products/firefox/switch.html" target="_blank" rel="nofollow">http://www.mozilla.org/products/firefox/switch.html</a>
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
Java exec does not enable piping commands- there are 2 work arounds I know

Take the InputStream from the firstProcess and write it to a file@

InputStream in = process.getInputStream()
stick it throuh a Buffered Reader into a BufferedWriter into the file;

The alternative and cheating method:
write something that takes a String[] of processes
make the first process set up the exec
then for every other element write the processes to the StdIn
OutputStream out = process.getOutputStream();
This means the Shell is up and running properly and other commands put to the command line are processed by the OS.
I can give some more details later if you need

Tom
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi guys,

As far as I can see,

works no differently to:

All you're doing is cutting out the "new StringTokenizer(command)" step described in the API docs.

However, running neither command under Windows XP created the text files. I tested this sort of thing about a week ago and never really came up with a satisfactory explanation of why some commands work and others don't.

Jules
Maarten Vergouwen
Ranch Hand

Joined: Jan 12, 2004
Posts: 60
Just out of curiosity (too lazy to try it), but would creating a batch file to run these commands, then executing the batchfile through the exec work?
Tom Hill
Ranch Hand

Joined: Aug 24, 2003
Posts: 115
Ive done that with Perl and unix - its a little bit of a hack for a solution, and there is a noticable overhead for opening 2 shells to do it (the batch shell) and the exec's shell.
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Hi guys,

It seems this is about half a zillion times more complex than it seems, i.e. the path to enlightenment is fraught with gotchas!

Have no fear the (surprisingly involved) solutions are at hand in a JavaWorld.com article entitled When Runtime().exec() won't. Thanks to fellow Rancher Craig Demyanovich for pointing me in the direction of this article in another thread.

The answer to Arun Thoppegowda Shivanna's original question is in there too. It's not really beginner stuff, however.

Jules
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to Run DOS Commands( ipconfig /All >> a.txt)