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 OverflowI 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
Do you want to execute code from .jar, or open it?
If open, then .jar file is the same format as .zip files and you can use zipfile module to manipulate it. Example:
def show_jar_classes(jar_file):
"""prints out .class files from jar_file"""
zf = zipfile.ZipFile(jar_file, 'r')
try:
lst = zf.infolist()
for zi in lst:
fn = zi.filename
if fn.endswith('.class'):
print(fn)
finally:
zf.close()
If you want to execute it then I prefer creating simple batch/shell script which execute java with some parameters like -Xmx and with environment settings required by application.
For the previous question, Executing it is easy.
import os
os.system('start X:\file.jar')
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
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!
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 yoursys.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 onsys.pathinto a single package). - A
.ziparchive containing any number of modules and packages can be stored anywhere, and its path added to yoursys.path(e.g., at runtime or viaPYTHONPATH) 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.
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.
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