• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

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

P.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
JulianInactive KennedyInactive
Ranch Hand
Posts: 823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic