We are building our applicatin as a Jar that needs to run Python scripts at runtime for some of its functionalities which are executed through Java code using the ProcessBuilder API.
Currently the Jar is expecting these Python scripts to be located in a specific directory on the system.
Is there a better way to package this Java application with the required Python scripts?
Asked
Active
Viewed 430 times
0

Harsh Pensi
- 113
- 7
-
Can't you pack the python script into the jar? Create a jar-directory called `scripts` and store your scripts there and update your manifest file. https://docs.oracle.com/javase/tutorial/deployment/jar/build.html - `ClassLoader cldr = this.getClass().getClassLoader(); java.net.URL scriptURL = cldr.getResource("scripts/myscript.py");` – paladin Nov 15 '21 at 07:55
-
https://stackoverflow.com/questions/51617058/how-to-include-static-files-in-jar see of tjos je;[s – JCompetence Nov 15 '21 at 08:18
-
@paladin, can you actually execute a script that is contained in a jar? Or do you first have to extract it to a temporary file so that the operating system can find the script? If scripts have to be extracted first, maybe it is an option to write a small wrapper for the Python scripts that looks into the jar and extracts the scripts on the fly. Something that works similar to `java -jar` but with Python. – Daniel Junglas Nov 15 '21 at 08:27
-
1@Daniel-Junglas You should be able to execute the script right from the jar (when called by the "jar" itself), if the user has sufficient rights and is able to call an external python interpreter. – paladin Nov 15 '21 at 12:16
-
@paladin Tried this but it says 'No such file or directory'. URL object is created though. `ClassLoader loader = this.getClass().getClassLoader(); java.net.URL scriptURL = loader.getResource("pyscriptTest.py"); ProcessBuilder pb = new ProcessBuilder("python3", scriptURL.getPath());` – Harsh Pensi Nov 16 '21 at 12:31
-
Have you configured your manifest file? – paladin Nov 17 '21 at 08:53
-
@paladin Have placed my python script in src/main/resources directory so maven packaged it into the Jar. Do I still need to change anything in the manifest file ? – Harsh Pensi Nov 17 '21 at 10:15
-
Would you please post your `META-INF/MANIFEST.MF` file? Would you please also manually check for your `pyscriptTest.py` being packed within the jar? You should be able to open the jar with any modern unzip-program, like winzip or unzip. – paladin Nov 17 '21 at 14:21
-
Yes have manually verified that pyscriptTest.py exists in the Jar and I am also able to create an InputStream with `this.getClass().getClassLoader().getResourceAsStream("pyscriptTest.py");` and extract it to an external file using Files.copy() method. Contents of MANIFEST.MF : Manifest-Version: 1.0 Created-By: Apache Maven 3.3.9 Built-By: harsh Build-Jdk: 11 – Harsh Pensi Nov 18 '21 at 09:47
-
You need to use streams (files) when interacting with external programs like your python interpreter. Or you might use an internal java based python interpreter like jython: `https://www.jython.org` – paladin Nov 18 '21 at 11:54
-
I was able to run the packaged python script in 2 ways, by creating external file as stated above in comments and by directing the stream to python's standard input as stated on the answer here - https://stackoverflow.com/a/62052574/4679013 – Harsh Pensi Nov 18 '21 at 12:06