0
import java.io.*;
class RunTest {
public static void main(String a[]) {
    try {
        String prg = "import sys\nprint int(sys.argv[1])+int(sys.argv[2])\n";
        BufferedWriter out = new BufferedWriter(new FileWriter("test1.py"));
        out.write(prg);
        int number1 = 1;
        int number2 = 2; 
        ProcessBuilder pb = new ProcessBuilder("python","test1.py",""+number1,""+number2);
        Process p = pb.start();
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        int ret = new Integer(in.readLine()).intValue();
        System.out.println("value is : "+ret);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

}

When I run this code (I'm using Eclipse), I get the stack trace:

java.io.IOException: Cannot run program "python": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048) at RunTest.main(RunTest.java:11) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(ProcessImpl.java:386) at java.lang.ProcessImpl.start(ProcessImpl.java:137) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029) ... 1 more

Anyone have any idea why and what I can do?

Thanks!

2
  • Is the python command in the path? Can you run it from the command line? Commented Jan 28, 2015 at 23:24
  • Python is in the path and I just checked and it does work from the command line, but still not in Eclipse... Commented Jan 29, 2015 at 15:49

2 Answers 2

0

You have to flush() and you should close() (which will also flush()) after you write to the File.

out.write(prg);
out.close(); // <-- add this.

Also, you'll need to add python to your PATH.

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

1 Comment

Woops sorry I took that line out when I was messing around trying to make it work before and forgot to put it back in, but it still has the same problem. Python is already in my path. Also, I
0

Sorry, this is probably really unhelpful, but somehow, it just started working. No idea why or how, because I haven't changed anything. Eclipse basically just restarted itself randomly and now it works! Sorry I couldn't post a solution that will help others, but thanks anyway for your help @MadProgrammer and @Elliott !

1 Comment

disappointing.... because i've got the same issue here. If anybody has any tips let's here them!

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.