0

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){

    }
}

Update

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??

1 Answer 1

1

Read the input & error stream in separated threads.

Or use the Commons Exec library : http://commons.apache.org/exec/

Sign up to request clarification or add additional context in comments.

1 Comment

The thing is I am using netbeans and it automatically starts up a GlassFish server and all the outputs where there the whole time...... So i kinda figured it out, but Thanks for the HELP!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.