JavaProcessBuilder freeze to execute external program
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Zahurul_Islam
Posted On:   Saturday, June 27, 2009 01:00 PM

I am trying to run external program using JavaProcessBuilder class. This program doesn't any status ( it freeze here: int status = mosesPro.waitFor();) when there are more than 20 lines in my test file (argument with -input-file). I would be very thankful for any solution/suggestion. I have attached my sample code below: String command [] = new String [5]; command [0] = "/Users/X/tools/moses/moses-cmd/src/moses"; command [1] = "-config"; command [2] = "/Users/X/Documents/thesis/workspace/juneTest/translation/evaluation/filtered.test/moses.ini"; command [3] = "-input-file"; command [4] = "/Users/X/Desktop/test2.input"; try    More>>

I am trying to run external program using JavaProcessBuilder class. This program doesn't any status ( it freeze here: int status = mosesPro.waitFor();) when there are more than 20 lines in my test file (argument with -input-file). I would be very thankful for any solution/suggestion. I have attached my sample code below:



			
String command [] = new String [5];
command [0] = "/Users/X/tools/moses/moses-cmd/src/moses";
command [1] = "-config";
command [2] = "/Users/X/Documents/thesis/workspace/juneTest/translation/evaluation/filtered.test/moses.ini";
command [3] = "-input-file";
command [4] = "/Users/X/Desktop/test2.input";
try {
child = new ProcessBuilder(command);
mosesPro = child.start();
int status = mosesPro.waitFor();
if (status == 0) {
BufferedReader reader = new BufferedReader(new InputStreamReader(mosesPro.getInputStream(), "UTF-8"));
String line;
line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
reader.close();

} else {
BufferedReader errorReader = new BufferedReader(new InputStreamReader(mosesPro.getErrorStream(), "UTF-8"));
String errLine;
errLine = errorReader.readLine();
while (errLine != null) {
System.out.println(errLine);
errLine = errorReader.readLine();
}
errorReader.close();

}
mosesPro.destroy();

} catch(Exception ex) {
ex.printStackTrace();
}
   <<Less

Re: JavaProcessBuilder freeze to execute external

Posted By:   Pavel_Nemec  
Posted On:   Monday, August 3, 2009 04:17 AM

Hm Read The Friendly Manual (RTFM)

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Process.html#waitFor()



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.





Another way. wait for will really freez your application. You probably want to read std in ant stdout. You should do it in Thread (so it will not block you main app.



Hope it helps.
About | Sitemap | Contact