Search code examples
javapythonjarexecute

How to embed a python script in a JAR/JAVA package?


I just call a script inside the Java code:

try {
    Runtime.getRuntime().exec("PlotFiles.py" + FinalCallStringCMD + " -w 1");
 } catch (IOException ex) {// catch ex   }

I have then two question:

1) How can I indicate the script in the code as part of the package and not as hardcoded path (ex. /users/pinc/Plot.py)

2) How can I embed the Python script in the JAR file, so I can always call it?


Solution

  • I would think you could do something similar to the solution in this question, which should solve both issues. I.E., put the Python file in some location below the src directory, and then load it using a relative path with the src directory as the "root", using the ResourcesLoader.

    I think you could call the getPath() method on the returned URL to achieve your needs, but I haven't tried this.