java - 如何使用 pyinstaller 在可执行文件中包含特定的 .jar 文件?

标签 java python jar pyinstaller

如何强制 pyinstaller 在打包为 exe 时使用特定的 .jar 文件?

我正在尝试生成一个使用 tabula-py lib 的可执行文件。该库需要一个 jar 文件 tabula-1.0.1-jar-with-dependency.jar,该文件位于我的 file.py 文件夹中。这些是 myfile.spec 中的一些修改:

# this is for pandas lib
def get_pandas_path():
    import pandas
    pandas_path = pandas.__path__[0]
    return pandas_path

dict_tree = Tree(get_pandas_path(), prefix='pandas', excludes=["*.pyc"])
a.datas += dict_tree
a.binaries = filter(lambda x: 'pandas' not in x[0], a.binaries)

# this is for tabula-py
jar = 'tabula-1.0.1-jar-with-dependencies'
jar_path = 'C:\\Users\\jaquedeveloper\\Documents\\freelancer\\bot\\' + jar
coll = COLLECT(exe,
       a.binaries,
       a.zipfiles,
       a.datas,
       [(jar, jar_path, 'PKG')],
       strip=None,
       upx=True,
       name='test')

但是,错误仍然存​​在。 当我从命令行运行代码时,来自 tabula-py 的 read_pdf() 函数(使用 java jar)运行得很好,完美。

但是当我使用 pyinstaller spec 命令生成可执行文件时,它无法执行此函数,并给出以下错误:

Unable to access jarfile C:\Users\jaquedeveloper\AppData\Local\Temp_MEI58442\tabula\tabula-1.0.1-jar-with-dependencies.jar

该文件不在该文件夹下,也不存在 tabula 文件夹。 我将该文件放在可执行文件的同一文件夹下。如何强制脚本使用它? 如何将 jar 从特定路径导入到可执行文件,而不是使用 _MEI 文件夹?

此问题也已报告 here .

最佳答案

我不久前偶然发现了这个线程,因为我遇到了完全相同的问题。使用 pyinstaller 将 .jar 文件添加到“onefile”可执行文件似乎将其放置在错误的临时目录中 - 在 ...AppData\Local\Temp_MEI58442\tabula\tabula-1.0.1-jar- 下with-dependency.jar。 添加的文件似乎放置在 ...AppData\Local\Temp_MEI58442\tabula-1.0.1-jar-with-dependency.jar 中。这就是为什么执行 tabula.read_pdf() 命令时找不到它。 作为解决方法,我使用 pyinstaller(而不是 onefile)生成了 onedir 输出,并添加了包含 jar 文件的目录“tabula”,并且它起作用了。

将其捆绑到单个文件中也可以: 实际上,这一切都在 pyinstaller 文档中( https://pyinstaller.readthedocs.io/en/stable/usage.html )。添加二进制 .jar 文件时,您可以在“:”(对于 Linux)和“;”之后指定临时文件夹内文件的相对路径。对于Win系统。仅使用“.” as path 将文件放置在 ./Temp_MEI*/下。所以在我的 Win 上,它看起来像这样:

pyinstaller --onefile --add-binary "C:/Users/Louis/Desktop/Github/pdf_reader/venv/Lib/site-packages/tabula/tabula-1.0.2-jar-with-dependencies.jar;./tabula/" myapp.py

这个解决方案来得有点晚,但我希望它仍然可以帮助别人。

关于java - 如何使用 pyinstaller 在可执行文件中包含特定的 .jar 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011932/

相关文章:

java - JMS 1.1 : Load sharing client side for Point-to-Point mode

java - c++ 编辑器的任何插件?

java - 在异步线程中解析时出现 fatal error (AsyncTask.doInBackground())

Python-标量的多个列表

python - 仅调用 python 返回代码状态

java - 将行号添加到已编译的类

java - 如何使 Firefox 驱动程序与手动打开 Firefox 一样

python - 为什么这个程序在猜出三次密码后还没有停止?

Java Eclipse : Read the code of a class imported from a library

java - jar 中的自定义文件类型