简体   繁体   中英

How To Bundle .jar Files with Pyinstaller

How do you get pyinstaller to bundle .jar files as archives for a python project that utilizes them?

For instance, to make an exe with (I am using pyjnius for handling the sikuli-standalone jar ):

# test.py
import os
import sys

# set the classpath so java can find the code I want to work with
sikuli_jar = '/sikuli-api.standalone-1.0.3-Pre-1.jar'
jarpath = os.path.dirname(os.path.realpath(__file__)) + sikuli_jar
os.environ['CLASSPATH'] = jarpath

# now load a java class
from jnius import autoclass
API = autoclass('org.sikuli.api.API')

Pyisntaller creates the ( one folder ) exe with:

pyinstaller -d test.py

But the jar to the best of my knowledge is not bundled and is inaccessible to the exe unless you manually place it in the folder generated by Pyinstaller

According to the Pyinstaller manual :

"CArchive contains whatever you want to stuff into it. It's very much like a .zip file."

I then try editing the previously auto-generated test.spec file with:

jar = 'sikuli-api.standalone-1.0.3-Pre-1.jar'
jar_path = 'C:\\Python27\\Lib\\site-packages\\sikuli-0.1-py2.7.egg\\sikuli\\' + jar
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               [('sikulijar', jar_path, 'PKG')],
               strip=None,
               upx=True,
               name='test')

And I try building the exe based on this spec file with:

python C:\workspace\code\PyInstaller-2.1\PyInstaller\build.py --onefile test.spec

But nothing happens and no error returns. Can someone provide a simple step by step tutorial how this could be done? Many thanks!

coll = COLLECT(exe,
           a.binaries,
           a.zipfiles,
           a.datas,
           [('sikulijar', jar_path, 'PKG')],
           strip=None,
           upx=True,
           name='test')

change 'sikulijar' in the tuple to just jar (the variable that you have already defined). you need to reference the same name that you have used in code.

However, I'm still trying to get the JVM to initialize properly. I'll post that if I figure that out.

With a valid virtual environment installed, here's how I've packaged a jar interoperating with python via jnius:

addFiles=" \
--add-data project/resources/jnius.so:jnius \
--add-data relative-path-to-jar.jar:resources \ # <-- example jar
--add-data any-other-resources:resources \
"

source ./venv/bin/activate
./venv/bin/pip install -r ./requirements.txt

./venv/bin/pyinstaller --onefile ${addFiles} project/mainModule.py --log-level WARN --hidden-import=jnius_config

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM