0

I am running a python script located in my Eclipse project folder with the following Java code:

Process p = Runtime.getRuntime().exec("python pythonscript.pyw");
p.waitFor();
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
System.out.println(in.readLine());

My console always reads null and my p.exitValue() is 2. However, typing the following in Windows command prompt works just fine and I get the correct output:

C:\users\user\workspace\project>python pythonscript.pyw

Does this have something to do with specifying my directory in Eclipse?

Daryl Yong
  • 11
  • 5
  • Yes, most likely. Make sure `python` is found by `exec`. (Related question: http://stackoverflow.com/questions/9368666/where-does-javas-processbuilder-look-to-execute-commands) – aioobe Jun 08 '15 at 08:24
  • When happens if you run Runtime.getRuntime().exec("path"); ? – NickJ Jun 08 '15 at 08:26
  • 2
    Your Java file and pythonscript.pyw file should be on the same directory for this to run.. – capt.swag Jun 08 '15 at 08:27
  • if you can try this outside of eclipse ,you can check is it work .make sure .class file and .pyw lying inside same folder – Madhawa Priyashantha Jun 08 '15 at 08:36
  • @aioobe I put python in my PATH variable so I'm pretty sure `exec` can find it, especially if the command prompt works – Daryl Yong Jun 08 '15 at 08:38
  • @DarylYong can you manually run and check is it work.create .java class inside a folder and put .pyw inside it .compile and run using cmd . – Madhawa Priyashantha Jun 08 '15 at 08:41
  • @4k3R I put the .pyw file together with the .java files in workspace\project\src\package and it doesn't work – Daryl Yong Jun 08 '15 at 08:43
  • @FastSnail I also tried putting the .pyw file together with the .class files in workspace\project\bin\package and it doesn't work – Daryl Yong Jun 08 '15 at 08:43
  • @DarylYong well plz add this code to your main method `System.out.println("Working Directory = " + System.getProperty("user.dir"));` and check is it same as where you put your python file. this will show you where you should put your python file – Madhawa Priyashantha Jun 08 '15 at 08:45
  • You can't put "pythonscript.pyw" inside your project src/class context, Python won't be able to find it, it must reside within a relative location to the execution context of your program. Try using ProcessBuilder instead, as it allows you to control the execution context of the process, amongst other things. A exit code of 2 yip ally means that the executable can't be found – MadProgrammer Jun 08 '15 at 08:48
  • 1
    You shuld put your pyw script to working directory of running java application. – cybersoft Jun 08 '15 at 08:52

2 Answers2

0

You can run such command:

String scriptDir = "./some dir";
Process p = Runtime.getRuntime().exec("cmd /c \"cd " + scriptDir + " && python pythonscript.pyw\"");
cybersoft
  • 1,453
  • 13
  • 31
0

Thanks to @aioobe for the link! After checking ProcessBuilder.environment() I realized that Python wasn't in there (even though it's in my PATH system environment variable, weird huh?) So I included the absolute path to my pythonw.exe and it worked just fine :) This is my edited code:

Process p = Runtime.getRuntime().exec("C:/python34/pythonw.exe pythonscript.pyw");
Daryl Yong
  • 11
  • 5