I would use subprocess this way:

import subprocess
subprocess.call(['java', '-jar', 'Blender.jar'])

But, if you have a properly configured /proc/sys/fs/binfmt_misc/jar you should be able to run the jar directly, as you wrote.

So, which is exactly the error you are getting? Please post somewhere all the output you are getting from the failed execution.

Answer from redShadow on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › running a .jar file through a python script
r/learnpython on Reddit: running a .jar file through a python script
May 25, 2013 -

This is what I am using right now

os.system("java -jar FULL_PATH\RR.jar")

A command prompt console window pops up for an instance and all i can read is 4 lines reading success followed by some other text.

Any help will be welcome

🌐
Reddit
reddit.com › r/learnpython › how do i call a jar file in python and execute code based off of the output of the jar?
r/learnpython on Reddit: How do I call a jar file in python AND execute code based off of the output of the jar?
April 22, 2018 -

Hi, I've got a .jar file that outputs a lot of text continuously. I've been using the following code to open the .jar in python.

import subprocess subprocess.call(['java', '-jar', 'prox.jar'])

I can see that all is working because I can see the output text in the IDLE, but I'm stuck on how to execute code based on the continuous output. So like...

if "test" in live_output_of_Jar: ... if "othertext" in... you get the idea.

Thanks!

🌐
Data Science Learner
datasciencelearner.com › python › how-to-call-jar-file-using-python
How to Call Jar File Using Python? - Python Tutorial Data Science Learner
October 27, 2022 - This article " How to call jar file using Python " will brief you on two different ways in Python to call jar externally (os.system and subprocess.call) .
🌐
Esri Community
community.esri.com › t5 › arcobjects-sdk-questions › using-a-java-jar-file-in-a-python-script › td-p › 299240
using a Java .jar file in a Python script - Esri Community
March 13, 2013 - Hello, I'm trying to write a Python script for ArcMap that will call a java .jar file. Within the Python script, if I can establish a connection to the .jar file, all I should need to do is write out one line of java code. I've been reading some of the responses to questions in this forum (in ...
🌐
SSEC
ssec.wisc.edu › ~tomw › visadtutor › compile.html
Compiling your Python code with jythonc
Make a batch file (or script) in the VisAD-Jython install directory to invoke the jythonc compiler. You can use the batch files in the VisAD-Jython installation directory as guidance. My script (which I called comp.bat) looks like this: @echo off set JYTHON_PATH=. set CLASSPATH=. %JYTHON_PATH%\jre\bin\java.exe -mx256m "-Dpython.home=%JYTHON_PATH%\\" "-Dpython .path=%JYTHON_PATH%.\\" -cp "%JYTHON_PATH%\\jython.jar;%JYTHON_PATH%\\visad.jar; %JYTHON_PATH%\\;." org.python.util.jython "%JYTHON_PATH%\Tools\jythonc\jythonc.p y " --compiler javac %1 %2 %3 %4 %5 %6 %7 %8 %9 (Note the "--compiler javac" option -- this assumes that the Java compiler to use (javac) is in your PATH.)
Find elsewhere
Top answer
1 of 4
38

Python doesn't have any exact equivalent to a .jar file.

There are many differences, and without knowing exactly what you want to do, it's hard to explain how to do it. But the Python Packaging User Guide does a pretty good job of explaining just about everything relevant.

Here are some of the major differences.


A .jar file is a compiled collection of classes that can be dropped into your application, or installed anywhere on your CLASSPATH.

In Python:

  • A .py (or .pyc) module can be dropped into your application, or installed anywhere on your sys.path, and it can be imported and used.
  • A directory full of modules can be treated the same way; it becomes a package (or, if it doesn't contain an __init__.py, it merges with other directories of the same name elsewhere on sys.path into a single package).
  • A .zip archive containing any number of modules and packages can be stored anywhere, and its path added to your sys.path (e.g., at runtime or via PYTHONPATH) and all of its contents become importable.

Most commonly, you want things to be installed into a system, user, or virtualenv site-packages directory. The recommended way to do that is to create a pip-compatible package distribution; people then install it (and possibly automatically download it from PyPI or a private repo) via pip.

pip does a lot more than that, however. It also allows you to manage dependencies between packages. So ideally, instead of listing a bunch of prereqs that someone has to go download and install manually, you just make them dependencies, and someone just has to pip install your-library. And it keeps track of the state of your site-packages, so you can uninstall or upgrade a package without having to track down the specific files.


Meanwhile, in Java, most .jar files are cross-platform; build once, run anywhere. A few packages have JNI native code and can't be used this way, but it's not the norm.

In Python, many packages have C extensions that have to be compiled for each platform, and even pure-Python packages often need to do some install-time configuration. And meanwhile, "compiling" pure Python doesn't do anything that can't be done just as well at runtime. So in Python, you generally distribute source packages, not compiled packages.

However, .wheel is a binary package format. You can pip wheel to build binary packages for different targets from the source package; then, if someone tries to pip install your package, if there's a wheel for his system, that will be downloaded and installed.

2 of 4
7

Easy Install from setup_tools defines the .egg format for deploying Python libraries or applications. While similar to JAR, it is nowhere spread as universally as JARs in Java world. Many people just deploy the .py files.

A newer format, intended to supersede eggs, is wheel.

🌐
DaniWeb
daniweb.com › programming › software-development › threads › 355378 › is-there-something-like-a-jar-file
python - Is there something like a jar file? [SOLVED] | DaniWeb
March 24, 2011 - Users can run it with python app.pyz, and on Windows you can associate .pyz so a double-click launches it. You can also bundle third-party deps into the archive (install them into a build directory first), similar to a fat JAR. See zipapp docs. If your users must run without installing Python, use a one-file packager that embeds the interpreter.
🌐
Google Groups
groups.google.com › g › jep-project › c › Ia_94lR2rMg
Including python code into jar file?
Apparently you can just add .jar file path to sys.path and it picks up from there. I ended up writing following code(Kotlin) this.javaClass.classLoader.getResources("mypackage/__init__.py").asSequence().forEach { url -> if (url.protocol == "file") { jepConfig.addIncludePaths(Paths.get(url.path).parent.parent.toString()) } else if (url.protocol == "jar") { val path = Paths.get(url.path).parent.parent.toString().trimEnd('!') jepConfig.addIncludePaths(path) } }
🌐
ActiveState
code.activestate.com › recipes › 577328-python-program-that-show-class-files-from-jar
Python program that show .class files from *.jar « Python recipes « ActiveState Code
Skip to Content · Community | Code | Docs | Downloads ▼ · Perl · Python · Tcl · Komodo IDE | more ▼ · Lists · Support · PPM Index · PyPM Index · Welcome, guest | Sign In | My Account | Store | Cart · ActiveState Code » Recipes · Languages Tags Authors Sets · This program iterate through .jar files and print .class files from it ·
🌐
Coderanch
coderanch.com › t › 769876 › languages › run-Python-dependency-libraries-running
Way to run Python with dependency libraries like running Java by making jar of them using pom.xml (Jython/Python forum at Coderanch)
February 4, 2023 - Like in Java if the program depends on some libraries, then those libraries are kept in the lib folder and using POM.xml, a jar is created which is a jar with dependencies and then it is run. Or another way is ... In Python if a program depends on some libraries (.Libraries can be downloaded from pypi.org )which are kept in the lib folder, what is the way to run the program with these libraries? ] thanks. ... With Python you may use a requirements file.
🌐
CodeBurst
codeburst.io › how-to-package-java-projects-in-python-tar-files-b9b3ff7a0627
How to Package Java Projects in Python Tar files | by Kartik Khare | codeburst
March 2, 2021 - Now, in the MANIFEST.in the file you need to tell python that the jars the directory needs to be packaged in the python package. You can do that by simply mentioning the command - ... This should create a directory dist inside the current directory and then add the python packagejava-integration-lib-1.0.0.tar.gz . You can now install this python package using the command · pip install dist/java-integration-lib-1.0.0.tar.gz · You can use the following code to test the package
🌐
Coderanch
coderanch.com › t › 600754 › languages › python-equivalent-Java-jar-file
What is the python equivalent to a Java .jar file? (Jython/Python forum at Coderanch)
December 19, 2012 - I've got a python application that is composed of dozens of source files. It works well if you connect into the directory with the source code and execute: ... But this is pretty user hostile and its fragile. If the user deletes one of the source code files, it breaks. If it were a java app, I'd create a jar file and just put the jar file in a good place and tell the user to do something like:
🌐
Stack Overflow
stackoverflow.com › questions › 19233700 › how-to-run-a-python-file-thats-inside-a-jar
java - How to run a python file that's inside a jar? - Stack Overflow
public class Bar { public static void main (String[] args) { String pythonPath = this.getClass().getResource("data/__hello__.py").getPath(); String command = new StringBuilder() .append("python ").append(pythonPath) .append(" -v ").append("201309") .toString(); CmdLineUtil.runFromCommandLine(command); } } When I run Bar without the JAR file, it works fine.
🌐
Klukas
jeff.klukas.net › home › lib.jar java library? python package? both?
lib.jar: Java library? Python package? Both? - Jeff Klukas
July 26, 2018 - If we have a working lib.jar containing our Java code, we can unzip it, add mylib/ at the root, and zip it back up. We can use this method to create a single file containing both Java/Scala code and relevant Python bindings.