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
🌐
GitHub
github.com › obriencj › python-javatools
GitHub - obriencj/python-javatools: Tools for examining Java bytecode in Python
A python module for unpacking and inspecting Java Class files, JARs, and collections of either.
Starred by 105 users
Forked by 30 users
Languages   Python 96.8% | Shell 1.1% | Makefile 1.0% | Java 0.6% | CSS 0.3% | Emacs Lisp 0.2% | Python 96.8% | Shell 1.1% | Makefile 1.0% | Java 0.6% | CSS 0.3% | Emacs Lisp 0.2%
🌐
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
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 ·
🌐
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

🌐
Medium
medium.com › @isaacramirez_86809 › a-simple-python-program-to-search-a-jar-in-maven-central-d7e9919758e9
A simple Python program to search a Jar in Maven Central | by Isaac Ramirez | Medium
July 20, 2020 - All the dependencies were stored in a folder readable by Ant and put into the WEB-INF/lib folder. One of the goals was to get rid of the folder that stores all the dependencies and rely on Gradle dependency management instead. Searching each Jar manually meant spending a lot of valuable time. This was one of those repeatable tasks that are perfect candidates for automation. I wrote a "quick and dirty" Python script that receives a directory containing jar files as an input, parses each file name and looks for the package name and a version.
🌐
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!

🌐
Reddit
reddit.com › r/learnpython › how to open folders and files inside a jar file without extracting it?
r/learnpython on Reddit: How to open folders and files inside a jar file without extracting it?
February 27, 2021 -

I want to build a script that will open jar files of Minecraft mods and extract information like the version and name of the mod from files inside them. The problem is that I can't seem to open text files and use them, or list the content of only certain folders from inside the jar file without having to extract it first.

I've installed the zipfile plugin, but have only been able to open the jar file itself. When I try to open a folder inside the jar file and list its contents I get an error "listdir: path should be string, bytes, os.PathLike or None, not ZipExtFile". Here's the code:

import os

from zipfile import *

mods_path = "D:\Coding\Python\Projekte\Minecraft Updater\mods"

os.chdir(mods_path)

test_mod = "Quark-r2.4-271.jar"

with ZipFile(test_mod, "r") as z:

with z.open("META-INF/") as folder:

os.listdir(folder)

It seems the listdir functions only works in normal folders. I haven't been able to find any documentation on methods for the ZipExtFile class to achieve what I'm trying to do. My concern is that the continuous extraction and deletion of files would impact the performance of my program, which I'm trying to keep as minimal as possible. Thanks in advance!

🌐
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) .
Find elsewhere
🌐
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 - 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 particular: http://forums.arcgis.com/threads/4883-java-.esriaddin-classpath?highlight=java+addin), and it sounds like there are a few issues I need to consider.
🌐
Better Programming
betterprogramming.pub › accessing-java-classes-in-python-using-pyjnius-6122bcaad49a
Accessing Java Classes in Python Using Pyjnius | by Eldad Uzman | Better Programming
March 8, 2022 - Pyjnius is based on the Java native ... into Python runtime. you must have Java installed. In my case, I used openJDK 11. You also need Javac installed (for example, apt-get install openjdk-11-jdk-headless). ... This code generates a reference to Java System class and then it invokes the out.println static method with the input ‘Hello world.`This should print ‘Hello world’ to the standard output. ... To use our costumed libraries, we first need to compile our Java code into a Jar ...
🌐
Stack Overflow
stackoverflow.com › questions › 45058776 › read-the-source-code-of-jar-files-with-python › 45060893
java - Read the source code of .jar files with python - Stack Overflow
July 12, 2017 - Krakatau is a command line tool in Python for decompiling .jar files, you can perhaps import it and use the relevant functions from inside your script.
🌐
MLJAR
mljar.com › docs › python-read-file
Read file in Python
Read content from a file in Python. Use `open()` function to open file and `read()` to get file contents into variable. File can be read in text or binary format.
🌐
Google Groups
groups.google.com › a › py4j.org › g › py4j › c › JNCiOJyrTKY
Accessing Java Class saved in Jar file from Python code
June 13, 2017 - Hi Juliette, Py4J only transmits messages between Python and Java. Each side is responsible for executing its respective code. Your java classes need to be executed in a JVM so you need to start the JVM to create an instance and call its methods. Py4J includes a few methods (e.g., JavaGateway.launch_gateway) to help you start a JVM.
🌐
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 - Thanks. I have also read something about .egg files, but I don't know them. What can you tell me about them? It seems that they are also another sort of compressed file (like .jar's). But, can you run the program without unpacking/installing? ... Python egg is the closest jar equivalent.
Top answer
1 of 3
4

get_path_lists uses recursion as so:

for d in dirs:
    get_path_list(d)

Namely, for every directory you do a full traversal of the whole tree: walk is already recursive! Or you would, if you actually passed in a full path os.path.join(root, d). You don't even use the result, so remove it.

Your

for f in files:
    if f.endswith(".jar"):
        app_list.append(root)
        break

can be simplified to

if any(f.endswith(".jar") for f in files):
    app_list.append(root)

I would convert get_path_list to an iterator:

def get_path_list(base_path):
    for root, dirs, files in os.walk(base_path):
        if any(f.endswith(".jar") for f in files):
            yield root

Success!

I would also consider a move to pathlib for its extended globbing:

def get_path_list(base_path):
    return set(path.parent for path in Path(base_path).glob("**/*.jar"))

although this comes with its own disadvantages.

get_app_name can be prettier more platform independent with pathlib.PurePath:

def get_app_name(lib_path):
    path = PurePath(lib_path)
    path = path.relative_to(path.anchor)

    return "-".join(path.parts)

The .relative_to stuff removes any drive or root, and then you can just join the parts.

In create_command you check if len(app_list) > 0; it's more idiomatic to just check if app_list.

You implicitly return None when empty; it seems to me you should be throwing an error. You should also use early exit to keep things cleaner.

Your formatting strings don't need numbers:

command = "{}{} Core -a {} -o {}".format(full_path, get_extension(), app_name, output_file)
for app_path in app_list:
    command += " -s {}".format(app_path)

Your for loop is non-idiomatic; you should never use += on immutable objects in a list without good reason. Here, just use:

command = "{}{} Core -a {} -o {}".format(full_path, get_extension(), app_name, output_file)
command += "".join(" -s {}".format(app_path) for app_path in app_list)

Even so, the real problem is that dependency_check uses shell=True; it shouldn't. Pass in a list instead:

command = [full_path + get_extension(), "Core", "-a", app_name, "-o", output_file]

for app_path in app_list:
    command.append("-s")
    command.append(app_path)

return command

The old method broke when any of the inputs had, say, spaces or asterisks in.

2 of 3
8
  • Ideally you should not have if conditions in your tests. You should always know exactly what it is that you want to test. test_app_name has a check on the platform. You should be able to mock it out and test the functionality under a 'windows' and a 'non-windows' environment. I'd split it out into different tests.

  • remove_old_files walks through a dir on your machine. Mock it out or include it in the repo for your code. Ideally mock it out if you don't really care about particulars of the files.

  • You have a lot of hardcoded paths in your code. This will not work on any other machine. You'll need some way of fixing this.

On a larger note:

You have the correct picture of a unit test, but you seem to try adn cover all cases under a single test. You need to test out all the methods, but you can split it out into multiple tests:

eg: Let's pick get_extension. There are 2 code paths in that method. So lets write 2 methods for it.

1. test_get_extension_windows
2. test_get_extension_non_windows

That should cover all cases for that method and we know exactly what gets executed in there. No need for conditions in our code. You can go on the same path for the other tests too.

🌐
Klukas
jeff.klukas.net › home › lib.jar java library? python package? both?
lib.jar: Java library? Python package? Both? - Jeff Klukas
July 26, 2018 - The Java distribution comes with a jar executable that can be used for looking inside or extracting a jar, but it’s not strictly necessary. unzip mylib.jar works just as well as jar xf mylib.jar. The usual method for installing a Python library is via pip which can handle downloading and installing a lib to an appropriate place on the filesystem such that the Python interpreter’s import mechanism will find it.
🌐
CopyProgramming
copyprogramming.com › howto › running-jar-files-from-python
Executing Jar Files using Python - Java
May 9, 2023 - Python's Ability to Execute Jar Files, What is the quickest method to execute a Java jar file using Python?, Invoking a Python script to access a container and obtaining accurate response statuses, Executing Java jar files using Python