How to pass file as an argument to Python process invoked by Java

293 views Asked by At

I am running Java program to call Python process using process builder as shown below,

    processBuilder = new ProcessBuilder(
                    Arrays.asList(
                    "/usr/bin/python",
                    "/opt/gui/oc_db5.py",
                    "-c",
                    "/opt/gui/test.json")   
            );

    processBuilder.directory(new File("/opt/gui"));

    processBuilder.start();

Location of python program is under /opt/gui directory and there is one test.json file also needs to be passed as parameter, with "-c" option, However what i am seeing is that system is appending location of java program with path of JSON file and then pick the .JSON file causing issue for Python code.

What actually python program is getting is /opt/java//opt/gui/test.json. I tried ../../ as well but it didn't work with test.json file.

Is there a way i can specify .JSON file as an argument to python program?

1

There are 1 answers

2
Aaron Esau On BEST ANSWER

This seemed to work for me. I mean, it fixed the directory problem.

try {
    int exitCode = Runtime.getRuntime().exec("python /opt/gui/oc_db5.py -c /opt/gui/test.json", null, new File("/")).waitFor(); // run program and get exit code
} catch(Exception e) { // is there an error?
    e.printStackTrace(); // print error
}