How To Bundle .jar Files with Pyinstaller

2019-06-23 17:56发布

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!

1条回答
欢心
2楼-- · 2019-06-23 18:23
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.

查看更多
登录 后发表回答