There’s zipapp from the standard library. See especially the section on Creating Standalone Applications with zipapp . There are some caveats, like not being able to ship C extensions. [image] zipapp — Manage executable Python zip archives Source code: Lib/zipapp.py This module provides… Answer from bschubert on discuss.python.org
🌐
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!

Discussions

how to compile a jar file from a py script?
how to compile a jar file from a py script More on github.com
🌐 github.com
9
July 27, 2022
java - Python: How can I execute a jar file through a python script - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I have been looking for an answer for how to execute a java jar file through python and after looking at: More on stackoverflow.com
🌐 stackoverflow.com
Call java class from a jar file in python easily without another complicated program like Py4j - Stack Overflow
I am trying to call java class from a jar file in python. I've see people talking about programs like Jython, Py4j, Jpype, etc, but I am hoping for an easier way other than spend lots of time to st... More on stackoverflow.com
🌐 stackoverflow.com
May 24, 2017
Import a java class in a python script - JAR directory?
I would like to import a java class in a python script. where is the directory to put my JAR ? More on forum.inductiveautomation.com
🌐 forum.inductiveautomation.com
0
0
November 11, 2011
🌐
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 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.
🌐
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 ...
🌐
Google Groups
groups.google.com › g › jep-project › c › Ia_94lR2rMg
Including python code into jar file?
import sys · sys.meta_path.append(zipimport.zipimporter("/path/to/file.jar")  · unread, Feb 24, 2021, 1:32:06 PM2/24/21 ·  ·  ·  · Reply to author · Sign in to reply to author · Forward · Sign in to forward · Delete · You do not have permission to delete messages in this group ·
Find elsewhere
🌐
GitHub
gist.github.com › ryanrichholt › f053a0277549967444d96126722458db
Python wrapper script for launching Java jar files · GitHub
Save ryanrichholt/f053a0277549967444d96126722458db to your computer and use it in GitHub Desktop. Download ZIP · Python wrapper script for launching Java jar files · Raw · java_wrapper.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
Stack Overflow
stackoverflow.com › questions › 27489109 › call-java-class-from-a-jar-file-in-python-easily-without-another-complicated-pro
Call java class from a jar file in python easily without another complicated program like Py4j - Stack Overflow
May 24, 2017 - Sign up to request clarification or add additional context in comments. ... This example just registers the jar file to the sys path. Is CPython required for Jython?
🌐
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 - Check if the find_modules.py is in the source directory. If yes, simply use the path of the jar in thetarget folder. If the find_modules.py is not in the source directory, it is being imported as a module.
🌐
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 - Unlike the subprocess module, os.system does not take arguments commas separated. Here we have to provide the space in order to differentiate between the arguments. As per the official documentation of Python 3 and my personal experience, I found subprocess is a better way to call jar files using Python.
🌐
Launchpad
answers.launchpad.net › sikuli › +question › 268512
Question #268512 “Import Python Module from Jar” : Questions : SikuliX
Further to it - I am using Sikuli-1.0.1, Sorry not to provide more specifications before. Our Framewrok is NOT yet fully designed, but this is what we got so far - 1. Driver Script - Must be .Sikuli folder 2. Action Script - Must be .Py Files 3. Images Repository 4. Framework Functions - Must be .Py Files - Should be in JAR 5.
🌐
Inductive Automation
forum.inductiveautomation.com › ignition
Import a java class in a python script - JAR directory? - Ignition - Inductive Automation Forum
November 11, 2011 - I would like to import a java class in a python script. where is the directory to put my JAR ?
🌐
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 - Is there any way to produce an executable file containing everything within it like Java's .jar's? ... If you want a single, double-clickable file that behaves like a JAR, Python’s built-in zipimport plus zipapp is the closest analogue.
🌐
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 file...
Top answer
1 of 2
4

In short, you can't run Java code natively in a CPython interpreter.

Firstly, Python is just the name of the specification for the language. If you are using the Python supplied by your operating system (or downloaded from the official Python website), then you are using CPython. CPython does not have the ability to interpret Java code.

However, as you mentioned, there is an implementation of Python for the JVM called Jython. Jython is an implementation of Python that operates on the JVM and therefore can interact with Java modules. However, very few people work with Jython and therefore you will be a bit on your own about making everything work properly. You would not need to re-write your vanilla Python code (since Jython can interpret Python 2.x) but not all libraries (such as numpy) will be supported.

Finally, I think you need to better understand the K-Means algorithm, as the algorithm is implicitly defined in terms of the Euclidean distance. Using any other distance metric would no longer be considered K-Means and may affect the convergence of the algorithm. See here for more information.


Again, you can't run Java code natively in a CPython interpreter. Of course there are various third party libraries that will handle marshalling of data between Java and Python. However, I stand by my statement that for this particular use case you are likely better to use a native Python library (something like K-Medoid in Scikit-Learn). Attempting to call through to Java, with all the associated overhead, is overkill for this problem, in my opinion.

2 of 2
2

To "answer" your question directly, Jython will be your best bet if you simply want to import Java classes. Jython strives very hard to be as compatible with Python 2.x as possible and does a good job. So you won't have to spend too much time rewriting code. Just simply run it with Jython and see what happens, then modify what breaks.

Now for the Python answer :D. You may want to use scikit for a native implementation. It will certainly be faster than running anything in Jython.

Update

I think the Py4J module is what you're looking. It works by running a server in your Java code and the Python code will communicate with the Java server. The only good thing about "Py4J" is that it provides the boiler plate code for you. You can very easily setup your own client/server with no extra modules. However I still don't think it's a superior option compared to Pythons native modules.

References

How to import Java class w/ Jython

Scikit - K-Means

🌐
Inductive Automation
forum.inductiveautomation.com › ignition
Importing JAR files - Ignition - Inductive Automation Forum
November 20, 2015 - I have searched the forum and have an idea on how to include third party Python libs, but what about third party JAR files? For example I want to use Jyson library to work with JSON. Where would I place this JAR file? …