2

I am trying to invoke a python script from java. The java compiles fine but the python script is not called.

public class javaToPython{


public static void main(String[] args) throws InterruptedException {
 try {      
Runtime runtime = Runtime.getRuntime();
Process process;


       process = runtime.exec("python /home/james/YCSB/bin/pythonycsbcommand.py ");


process.waitFor();
       } 
 catch (IOException ex) {
       Logger.getLogger(javaToPython.class.getName()).log(Level.SEVERE, null, ex);
   }
   }
}

python script:

import os

fo = open("/home/james/YCSB/bin/command.txt", "r+")
str = fo.read()

print "", str

fo.close()

os.system(str)

Edit:

I have tried using the process builder but again with the same results.

ProcessBuilder pb = new ProcessBuilder("python", "/home/james/YCSB/bin/script.py", "-m 2");
Process p = pb.start();

I have tried entering a path that does not exist but the java script still compiles.

JamesF
  • 197
  • 1
  • 2
  • 10
  • You'll get a `NullPointerException`, since `process` is `null` when you call `process.getOutputStream()`. – Andy Turner Apr 01 '15 at 22:48
  • Sorry, I uploaded the wrong edit. Have edited question – JamesF Apr 01 '15 at 22:52
  • possible duplicate of [Java Runtime.exec() arguments on Linux](http://stackoverflow.com/questions/14533954/java-runtime-exec-arguments-on-linux) – Andy Turner Apr 01 '15 at 22:56
  • How do you know that the script is not called? (please be specific when describing the command you try to run). What happens instead? – that other guy Apr 01 '15 at 22:57
  • Also, as suggested in a comment on the duplicate question, consider using `ProcessBuilder`. – Andy Turner Apr 01 '15 at 22:58
  • `exec(String)` should never be used, but this is not a problem with arguments. It's probably either because the program is outputting something that's not being read, or because there's a problem with the command being executed. – that other guy Apr 01 '15 at 23:06
  • There is no problem with the command being executed as it works perfectly when I exe it manually. ie, go into term and type "python pythonycsbcommand.py". – JamesF Apr 01 '15 at 23:11
  • As far as I can tell, the java does absolutely nothing because I can put a path that does not exist and it will still execute with 0 error – JamesF Apr 01 '15 at 23:27
  • Working fine ; Tried with pymsgbox – Smart Manoj Mar 11 '19 at 14:55

0 Answers0