0

How can I force pyinstaller to use specific .jar file when packing as exe?

I am trying to generate an executable which uses tabula-py lib. This library requires a jar file, tabula-1.0.1-jar-with-dependencies.jar, which I have in my file.py folder. These are some modifications which are at 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')

Still, the error persists. When I run my code from command line, the function read_pdf(), from tabula-py, which uses the java jar, works all right, perfectly.

But when I generate the executable with pyinstaller spec command, it fails to execute this function, giving the error below:

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

The file is not under this folder, nor the tabula folder exists. I have the file under the same folder of the executable file.How can I force the script to use it? How can I import the jar from a specific path to the executable file, instead of using _MEI folder?

This issue was also repported here.

Jaqueline Passos
  • 1,301
  • 2
  • 24
  • 37
  • Hi Jaqueline. Did you have any luck finding an answer to this? I have a similar problem. – Paul Jan 12 '19 at 06:46
  • No luck, I ended up not developing the functionality due to a client request and did nothing about it since them... I have generated an exe but not using tabula, because there was no more necessity to filter the pdf files. – Jaqueline Passos Jan 20 '19 at 20:04
  • 1
    Ok. Thanks for letting me know. I ended up rewriting my program using Camelot-py instead of Tabula so that a user without admin rights could run it without Java. – Paul Jan 21 '19 at 05:30

1 Answers1

9

I stumbled upon this thread a while ago, since I ran into the exact same Problem. Adding the .jar file to a 'onefile' executable with pyinstaller seems to place it in the wrong temporal directory - under ...AppData\Local\Temp_MEI58442\ tabula \tabula-1.0.1-jar-with-dependencies.jar. The added File seems to be placed in ...AppData\Local\Temp_MEI58442\tabula-1.0.1-jar-with-dependencies.jar. That's why it can't be found when executing the tabula.read_pdf() command. As a workaround I produced a onedir output with pyinstaller (instead of onefile) and added a directory 'tabula' with the jar file, and it worked.

Bundling this into a single file also works: It's actually all there in the pyinstaller docs (https://pyinstaller.readthedocs.io/en/stable/usage.html). When adding the binary .jar file you can specify the relative path of the file inside the temporary folder after ':' for Linux and ';' for Win Systems. Using just '.' as path places the file under ./Temp_MEI*/. So in my case on Win it looked like this:

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

This solution comes a little late, but i hope it still helps somebody.

Louis Wagner
  • 106
  • 1
  • 4
  • Thanks, I've used the same solution and didn't document it here. It will for sure help somebody. – Jaqueline Passos Dec 17 '19 at 16:09
  • I tried this fix on my debian laptop and got an error: ```pyinstaller: error: argument --add-binary: invalid add_data_or_binary value: '/home/glicka/anaconda3/lib/python3.9/site-packages/tabula/tabula-1.0.5-jar-with-dependencies.jar;./tabula/'``` Do you know what I did wrong? My attempt: ```pyinstaller --onefile --add-binary "/home/glicka/anaconda3/lib/python3.9/site-packages/tabula/tabula-1.0.5-jar-with-dependencies.jar;./tabula/" src/pdf2xlsx.py ``` – Adam G. Mar 29 '22 at 21:52
  • Might be a little too late, but if you read carefully, it says you need to use ':' instead of ';' on linux to separate dependencies – Louis Wagner Mar 30 '23 at 08:01