0

Heres my code

public void addImg(){
    try{
        //Attempt 1
        Runtime r = Runtime.getRuntime();
        Process p = r.exec("/usr/bin/python2.7 ../wc.py");
        p.waitFor();
        p.destroy();

        //Attempt 2
        p = r.exec("python2.7 ../wc.py");
        p.waitFor();
        p.destroy();
    }catch (Exception e){
        String cause = e.getMessage();
        System.out.print(cause);
    }
}

Ive been trying to get this to work for about an hour no and it seems as though nothing is working, and no error is displayed. Im more concerned with how I would go about debugging this, but is there anything wrong with my code that would indicate why this script isnt executing?

1
  • Absolute path to your .py file would the first thing I'd try. Commented Oct 14, 2013 at 17:10

3 Answers 3

2

If the exec() method does not throw an exception straight away it simply means it could execute the external process. This however does not mean it executed successfully or that it even executed properly.

There are many ways to check if a external process executed successfully in which few are listed below:

Use the Process.getErrorStream() and Process.getInputStream() methods to read output from the external process.

Look at the exit code the external process, a code 0 represents normal execution, otherwise an error probably occurred.

Consider adding the following code for debugging purposes:

public void addImg(){
    try{
        Runtime r = Runtime.getRuntime();

        //Don't use this one...
        //Process p = r.exec("/usr/bin/python2.7 ../wc.py");
        //p.waitFor();
        //p.destroy();

        //Use absolute paths (e.g blahblah/foo/bar/wc.py)
        p = r.exec("python2.7 ../wc.py");

        //Set up two threads to read on the output of the external process.
        Thread stdout = new Thread(new StreamReader(p.getInputStream()));
        Thread stderr = new Thread(new StreamReader(p.getErrorStream()));

        stdout.start();
        stderr.start();

        int exitval = p.waitFor();
        p.destroy();

        //Prints exit code to screen.
        System.out.println("Process ended with exit code:" + exitval);
    }catch(Exception e){
        String cause = e.getMessage();
        System.out.print(cause);
    }
}

private class StreamReader implements Runnable{
    private InputStream stream;
    private boolean run;

    public StreamReader(Inputstream i){
        stream = i;
        run = true;
    }

    public void run(){
        BufferedReader reader;
        try{
            reader = new BufferedReader(new InputStreamReader(stream));

            String line;

            while(run && line != null){
                System.out.println(line);
            }
        }catch(IOException ex){
            //Handle if you want...
        }finally{
            try{
                reader.close();
            }catch(Exception e){}
        }
    }
}

Also, try looking into using ProcessBuilder when invoking external applications, I find it to be much easier to use despite the more code required.

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

5 Comments

I get an exit status of 1, and someone mentioned that it may be because ../ isnt resolved in exec(), how could I reslove it?
@JakeSchievink Use the full path of the script. ../ is a relative path relating to the current working directory. Avoid doing this and state the full path (starting from the root). I don't have much experience with linux but on windows it typically starts with a drive letter such as C: An example of an absolute path would be C:\scripts\wc.py
Thanks for all your help, now when I run it it never gets past the waitFor() method
@JakeSchievink Well is it actually doing its job? Not getting past the waitFor() method means the external application is not closing as the waitFor() method blocks on the termination of the application. What does the output of the external application show?
Added an exit() command to the end of my python script, works well thanks again, have a grand day
0

You need to look at the result returned by running the command instead of just catching exceptions.

Take a look at the exitValue() and methods to get the output and error streams on your Process object.

My guess is that python isn't able to find your program because ../ is resolved by your shell and programs launched using exec aren't run from a shell.

1 Comment

the exit status returns 1
0

Print out the error stream:

Runtime r = Runtime.getRuntime();
String line;
Process p = r.exec("/usr/bin/python2.7 ../wc.py");
InputStream stdin = p.getErrorStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
p.waitFor();
while ( (line = br.readLine()) != null)
    System.out.println("-"+line);
p.destroy();

probably wc.py is not found.

Comments

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.