Reputation: 2067
I've created a Java WebApp, more like i'm using HTTPServlets to run some java code depending on what has been entered by the user. I can get the information from the user, pass it to my java program and pass it back to the interface no problem. What has become difficult (and rather annoying) is trying to use the information input by the user and calling an external script (which is needed to process and answer the users request). I've tested that part of my code in an regular stand alone java application and it works fine, but whenever I try to run it through my web app it either hangs forever (I don't know if its really running the script) or doesn't even run the script and just continues to the result page.
Here's my Code:
public void runScript() throws InterruptedException{
try{
chkPy = new BufferedWriter(new FileWriter("Log.txt"));
chkPy.write("Running Python Script");
chkPy.newLine();
runtime = Runtime.getRuntime();
p = runtime.exec("cmd /c run.bat > python_results.log");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = "";
while ((line = stdInput.readLine()) != null) {
chkPy.write(line);
chkPy.newLine();
}
while ((line = stdError.readLine()) != null) {
chkPy.write(line);
chkPy.newLine();
}
int exitVal = p.waitFor();
chkPy.write("Exit Value" +exitVal);
chkPy.flush();
chkPy.close();
}catch(IOException ex){
}
}
Netbeans comes with an implementation of GlassFish servers and as stupid as this sounds it wasn't the first place i looked (I only looked now), but the scripts are compiling. But it seems i have run into another problem as now the webapp deadlocks eventhough the script has finished writing the files.... how do I get around this??
Upvotes: 0
Views: 826
Reputation: 21564
Read the input & error stream in separated threads.
Or use the Commons Exec library : http://commons.apache.org/exec/
Upvotes: 1