0

I am trying to run a python script using runtime in java, but the command doesn't work, I got this error:

Cannot run program "python": CreateProcess error=2, The system cannot find the file specified

Even i set the PATH variable with the executable python.

public void call(){

  String command= "python C:/Python27/lib2.py";       
   Process p = Runtime.getRuntime().exec(command);

}

PATH = "C:\Python27"
6
  • 1
    Can you run the command manually from command line? Can you please show us your PATH? Commented Sep 5, 2016 at 14:07
  • Make sure that python is in the PATH where you're running the build, or specify an absolute path to your python executable in the exec task. Then take a look at this Commented Sep 5, 2016 at 14:08
  • Yes, it works fine manually from command line, i already set the PATH, but the error still present Commented Sep 5, 2016 at 14:11
  • Try getting rid of 'python' and just executing the file Commented Sep 5, 2016 at 14:32
  • This might be a tokenizer problem. Could you try running exec with new String[] { "python", "C:/Python27/lib2.py" } as an argument? Commented Sep 5, 2016 at 14:46

1 Answer 1

1

Have a look at ProcessBuilder. The sample code shows:

ProcessBuilder pb =
   new ProcessBuilder("myCommand", "myArg1", "myArg2");
 Map<String, String> env = pb.environment();
 env.put("VAR1", "myValue");
 env.remove("OTHERVAR");
 env.put("VAR2", env.get("VAR1") + "suffix");
 pb.directory(new File("myDir"));
 File log = new File("log");
 pb.redirectErrorStream(true);
 pb.redirectOutput(Redirect.appendTo(log));
 Process p = pb.start();
 assert pb.redirectInput() == Redirect.PIPE;
 assert pb.redirectOutput().file() == log;
 assert p.getInputStream().read() == -1;

You could do env.put("PATH", "c://python/bin"); or wherever your python installation may be found, to ensure that python is found in path.

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

1 Comment

the same problem can not run th program "python"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.