• 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

On running external programs in java

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, i need some help with my code, i'm trying to run several .exe files, i am currently using Runtime.getRuntime().exec. It works fine with the first exe command, however it stops there and does not perform the other commands following it.
here is a sample of my code:
============================================================================
Runtime p = Runtime.getRuntime();
p.exec("randinit -din skin.dat -cout skin.cod -xdim 100 -ydim 96 -topol hexa -neigh bubble -rand 123");
System.out.println ("Initialization Done");
//first map training
Runtime p1 = Runtime.getRuntime();
p1.exec("vsom -din skin.dat -cin skin.cod -cout skin.cod -rlen 1000 -alpha 0.05 -radius 10");
System.out.println ("First Training Done");
============================================================================
this either only runs the initialization part but does not perform the training part or it performs both simultaneously, therefore not producing the correct results since what i need is for them to run one after the other. How can i do this? please help. thank you very much.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: our naming policy requires that you use a real first and last name, not a "handle", for your display name. If you could head over here and fix that for me, I'd be much obliged.

Second, on to your question: you probably want to wait until the first program is done before starting the second one. use the waitFor() method of Process to wait for a Process to complete.
 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. i'm sorry for the multiple post error. i didn't know where to put my question and was afraid of being fined for posting in the wrong section. anyway, can you give me an example of how i can use wait? I am not familiar with the syntax and I can't find any (yet) sample code which uses it. What does wait do? thanks.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method is Process.waitFor() -- Object.wait() is a different beast. The following is from the JavaDocs. Get familiar with them as they will become your best friend when you have questions.

public abstract int waitFor() throws InterruptedException

causes the current thread to wait, if necessary, until the process represented by this Process object has terminated. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked until the subprocess exits.

First, there should be a single Runtime for the JVM as a whole; there's no point in grabbing a reference to it twice and storing it into two different variables named p and p1. Second, Runtime.exec() returns a Process that you'll need to save a reference to in order to use wairFor().
 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your help - i used waitFor () and it indeed helped me out in running these exe commands one after the other, however i have another probem: i need to run another exe file several times (still one after the other), this exe file takes in 3 arguments: here is the syntax of how the exe file is called:

visual -din img100Scene4.dat -cin skin.cod -dout img100Scene4output.dat

img100Scene4.dat, skin.cod and img100Scene4output.dat are the arguments needed, running this exe file with Runtime and waitfor works well if i do this:

try {
Runtime rt = Runtime.getRuntime();
Process p1 = rt.exec ("visual -din img100Scene4.dat -cin skin.cod -dout img100Scene4output.dat");
p1.waitFor ()
} catch (Throwable t) {
t.printStackTrace();
}

but in running this exe file repeatedly i have to put it inside a loop and constantly change the label img100Scene4 so i did this:

while (//terminates after several iterations)
try {
//label is a string which changes value here
Runtime rt = Runtime.getRuntime();
Process p1 = rt.exec ("visual -din" + label + ".dat -cin skin.cod -dout" + label + "output.dat");
int length = p1.waitFor ();
System.out.println (length);
} catch (Throwable t) {
t.printStackTrace();
}
}

this code (on its first call) immediately prints out length = -1 which means that the process did not end. it is like this every time it loops back and tries to execute p1. how do i solve this? thank you. I really appreciate all the help.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like from your example command line there needs to be space after "-din" and after "-dout", but if the code you're showing is the exact code you're using, you forgot to include those spaces when you assemble the command lines at runtime.
 
christine clarin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very much, i actually corrected that mistake right after i posted here. sorry for the inconvenience. thanks, you guys helped me a lot. i just recommended this forum to one of my thesis groupmates. thanks again. till next time!
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I'm also using the same method (waitFor) but it doesn't seem to work. Can someone tell me what I'm doing wrong here. Here's a part of my code:

//call on connected components
Runtime rt = Runtime.getRuntime();
// initialize
Process proc1 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+1+".txt");
Process proc2 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+1+".txt");
Process proc3 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+2+".txt");
Process proc4 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+2+".txt");
Process proc5 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+3+".txt");
Process proc6 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+3+".txt");
Process proc7 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+4+".txt");
Process proc8 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+4+".txt");
Process proc9 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+5+".txt");
Process proc10 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+5+".txt");
Process proc11 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+6+".txt");
Process proc12 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+6+".txt");
Process proc13 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+7+".txt");
Process proc14 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+7+".txt");
Process proc15 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+8+".txt");
Process proc16 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+8+".txt");
Process proc17 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+9+".txt");
Process proc18 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+9+".txt");
Process proc19 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+10+".txt");
Process proc20 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+10+".txt");
Process proc21 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+11+".txt");
Process proc22 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+11+".txt");
Process proc23 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+12+".txt");
Process proc24 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+12+".txt");
Process proc25 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+13+".txt");
Process proc26 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+13+".txt");
Process proc27 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+14+".txt");
Process proc28 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+14+".txt");
Process proc29 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+15+".txt");
Process proc30 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+15+".txt");
Process proc31 = rt.exec("java.exe ConnectedComponents Blood "+outFileName+16+".txt");
Process proc32 = rt.exec("java.exe ConnectedComponents Skin "+outFileName+16+".txt");

try
{
proc1.waitFor();
proc2.waitFor();
proc3.waitFor();
proc4.waitFor();
proc5.waitFor();
proc6.waitFor();
proc7.waitFor();
proc8.waitFor();
proc9.waitFor();
proc10.waitFor();
proc11.waitFor();
proc12.waitFor();
proc13.waitFor();
proc14.waitFor();
proc15.waitFor();
proc16.waitFor();
proc17.waitFor();
proc18.waitFor();
proc19.waitFor();
proc20.waitFor();
proc21.waitFor();
proc22.waitFor();
proc23.waitFor();
proc24.waitFor();
proc25.waitFor();
proc26.waitFor();
proc27.waitFor();
proc28.waitFor();
proc29.waitFor();
proc30.waitFor();
proc31.waitFor();
proc32.waitFor();
}
catch ( InterruptedException e )
{
System.err.println(e);
return;
}
System.out.println("connected components completed!");


+-+-+-+-+

I would also like to add that the executed external java program also calls on yet another java program. I also used waitFor() on this program. Would that be the problem?
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,

Not sure why you are using Runtime.exec() if you're calling other java classes. Why not just make method calls?

You don't explain what you mean by "it doesn't seem to work".
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
And why don't you use an loop?


(corrected typo: '(Blood "' to '("Blood "').
[ February 08, 2005: Message edited by: Stefan Wagner ]
 
michael echavez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the suggestions!!!
 
michael echavez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! A big big thanks to the people who responded immediately, your suggestions helped a lot! I didn't use runtime exec anymore and I used a for loop to shorten my code. I would also like to thank Ms. Clarin for helping me with this personally. Thanks!!!
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic