• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

running external commands in java

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I want to run external commnad from my program.
I have a utility which I use to find differences between two files.
Its a console application and can be run from command line by

>diff file1 file2
I want to call this from a java program. How can I do this.
Thanks
Avijit
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How interesting you put this in the streams forum because streams are the SECOND part of the answer.

The first part is Runtime.getRuntime().exec( command ). This will run an external command. It's likely to tie you to one OS and requirements of external programs, but many people find they can live with those restrictions.

exec returns a Process object, which has some streams. You need to hook up and read the stdout and errout streams. We often make separate threads for the two readers. Search this forum or the threads forum for "streamgobbler" to find some neat examples posted in the past.

I don't know if all that made sense or left you scratching your head. See how far you can get with the JavaDoc and some code. Then show us the parts you understand the the parts you don't and we'll try to keep you moving forward.
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
I tried what you said.
Process p1 = Runtime.getRuntime().exec("dir");
It did not work. Then I did some research.
Process p1 = Runtime.getRuntime().exec("cmd /c dir");
It worked this time.
Could you tell me what exactly this additional cmd /c doing??
I was succesful runnning a batch file also using this
Runtime.getRuntime().exec().
I will go thorugh the javadoc thoroughly as you said.
Avijit
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ May 10, 2005: Message edited by: Joe Ess ]
 
Neel Chow
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks..we use windows day in and day out...and still how little we know about it...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic