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!
how to compile a jar file from a py script?
java - Python: How can I execute a jar file through a python script - Stack Overflow
Call java class from a jar file in python easily without another complicated program like Py4j - Stack Overflow
Import a java class in a python script - JAR directory?
Videos
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.
This always works for me:
from subprocess import *
def jarWrapper(*args):
process = Popen(['java', '-jar']+list(args), stdout=PIPE, stderr=PIPE)
ret = []
while process.poll() is None:
line = process.stdout.readline()
if line != '' and line.endswith('\n'):
ret.append(line[:-1])
stdout, stderr = process.communicate()
ret += stdout.split('\n')
if stderr != '':
ret += stderr.split('\n')
ret.remove('')
return ret
args = ['myJarFile.jar', 'arg1', 'arg2', 'argN'] # Any number of args to be passed to the jar file
result = jarWrapper(*args)
print result
Here your first problem is with your java version with which you have compiled the code and java version with which you are running the code.
For example, if you have compiled the code with java version 8 and you are running the java application with java version 7 or 6 (lower than compiled one) you will get a Unsupported major.minor version 52.0 error. Hence compile the code with a lower or same version than your server one.
Check the version in your server : java --version
Check the version in your development tool with which you have compiled the code
In the below code, provide full path to your jar file as well.
os.system("java -jar \fullpath\PGPEncryption.jar BC.csv.pgp X.csv <password>")
If you want to use modules in JAR file in python code, the you need to run that py file using JYTHON.
java -jar jython.jar demo.py
Jython download
Check here for tutorial
Subprocess with Popen
import subprocess
x = subprocess.Popen("java -jar PGPEncryption.jar BC.csv.pgp X.csv <password>", stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out,err = x.communicate()
print "Error is - ",err
print "Output is -",out
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.
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