Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Arun thoppegowda shivanna
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Hill
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic