0

I am trying to write a Java application that executes a python script in order to return a value to the original program. However, the script is written in Python3 so I can't use Jython.

My current program works fine in Intellij but when I export to a JAR file it no longer works (I'm assuming because the filepath is different). I know questions similar to this have been asked in the past but none of the solutions seem to be working.

String currentPath = new File("").getAbsolutePath();
String path = Paths.get(currentPath, "src", "com", "engine", "pythonScript.py").toUri().toString();

String[] cmd = new String[]{"python", path, data};
try {
    Process p = Runtime.getRuntime().exec(cmd);

    BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String output;

    while ((output = stdInput.readLine()) != null) {
        System.out.println(output);
        if (checkOutput(output)) {
            return output;
        }
    }

    BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
    String s;
    while ((s=error.readLine()) != null) {
        System.out.println(s);
    }

} catch (IOException e) {
    e.printStackTrace();
}
  • [This](https://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java) might help you: this way you can get the path of the executing jar file: – Miguel Garnacho Vélez Jan 28 '19 at 22:54
  • Thanks for the response. I'll look into it – Crackers42 Jan 29 '19 at 22:09
  • I have managed to get the correct file path for the python file within the jar. But the runtime execution is saying that the file path doesn't exist. I've found it's the same issue if I try and use the command prompt to cd to the file path – Crackers42 Jan 30 '19 at 22:53

0 Answers0