Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a java JAR file from python

Tags:

python

jar

I am trying to open a JAR file from python and running into problems. I am using..

import os
os.system(r"X:\file.jar")

it appears to open the window then it shuts right away, I know I am missing a simple command but not sure what it is, thanks for the help

like image 606
Trying_hard Avatar asked Nov 04 '11 05:11

Trying_hard


1 Answers

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.

like image 124
Michał Niklas Avatar answered Oct 08 '22 06:10

Michał Niklas