Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homebrew installed Tesseract not detected by Tess4J #194

Open
nickjwhite opened this issue Aug 11, 2020 · 11 comments · May be fixed by #240
Open

Homebrew installed Tesseract not detected by Tess4J #194

nickjwhite opened this issue Aug 11, 2020 · 11 comments · May be fixed by #240

Comments

@nickjwhite
Copy link

Homebrew on OSX installs Tesseract in /usr/local/Cellar/tesseract/, and it puts symlinks for the libraries in /usr/local/lib. However it doesn't seem like JNI picks up the libraries in this location, with the error "Native library (darwin/libtesseract.dylib) not found in resource path".

I'm using VietOCR, so I temporarily fixed the issue by unpacking the .jar, and copying libtesseract.dylib into a darwin directory within it, and re-zipping it. However I think a better solution would be to use NativeLibrary.addSearchPath() to search for /usr/local/lib, as this is a common way of installing Tesseract on OSX.

I'm not very familiar with Java or OSX (I found this issue while helping someone else), so it's possible that there's a better way to do this, but the above suggested solution is the best I know of.

@nguyenq
Copy link
Owner

nguyenq commented Aug 12, 2020

JNA looks for the native libraries in certain system paths as described in https://java-native-access.github.io/jna/4.2.1/com/sun/jna/NativeLibrary.html (code). I would think /usr/local/lib would be among them. But to be certain, you can run the program with the system property jna.debug_load=true to print out the search paths.

java -Djna.debug_load=true -jar VietOCR.jar

@nguyenq
Copy link
Owner

nguyenq commented Aug 12, 2020

@nickjwhite I just came across this post at JNA Groups board and wonder it has something to do with the issue you're experiencing on MacOS. I may need to make a new release to incorporate JNA 5.6.0 version, which addresses a Mac-related bug.

@nickjwhite
Copy link
Author

Thanks @nguyenq, that's very helpful. I also would have thought that /usr/local/lib would be one of the system paths jna checks, but I got my friend to run VietOCR with the jna.debug_load=true setting as you suggested, and sure enough the relevant line is just:

INFO com.sun.jna.NativeLibrary - Adding system paths: [/usr/lib, /usr/lib]

The post you found about the MacOS changes, and JNA's response to them, is certainly interesting. These issues were on OSX 10.14.6, whereas those bugs are triggered with OSX 11.x, however it may possibly be the case that JNA 5.6.0's different way of probing for libraries would fix this case as well. I suspect it wouldn't, though, to be honest.

@nguyenq
Copy link
Owner

nguyenq commented Aug 12, 2020

So it turned out that JNA did not look at the directory we expected, but there's a way to tell it where to look for the native library by setting jna.library.path system property, as follows when you launch the program:

java -Djna.library.path=/usr/local/lib -jar VietOCR.jar

where /usr/local/lib would have libtesseract.dylib symlink.

Or create the symlink in /usr/lib directory, if possible.

https://github.com/java-native-access/jna/blob/master/www/GettingStarted.md

@nguyenq
Copy link
Owner

nguyenq commented Aug 13, 2020

@nickjwhite VietOCR-5.6.3, which uses JNA-5.6.0, has just been released. Please see if it has resolved the issue with loading Tesseract dynamic library on MacOS.

Thanks,
Quan

@nickjwhite
Copy link
Author

@nguyenq thanks for that, good idea. Unfortunately the new version doesn't fix this; surprisingly /usr/local/lib doesn't seem to be checked by JNI on OSX at all. Previous versions of VietOCR I just set the Tesseract executable location, so never thought about detection of system libraries, but presumably that wouldn't have worked back then either.

I have no experience with Java development, I'm afraid, so I can't provide a patch adding the NativeLibrary.addSearchPath() which should hardcode a fix. I think, inelegant as it is, it would be worth doing, as Homebrew is a common and recommended way of installing Tesseract on OSX.

Thanks for your continued help with this.

@nguyenq
Copy link
Owner

nguyenq commented Aug 17, 2020

Is this applicable?

https://support.azul.com/hc/en-us/articles/360039650212-On-MacOS-10-15-3-and-later-Zulu-Java-doesn-t-load-the-JNI-Libraries-my-Application-needs

You may want to post the question in JNA Groups and see if there's any explanation for the path exclusion.

Thanks.

@RickardRanniger
Copy link

I solved it by adding this inside my main method. System.setProperty("jna.library.path", "/usr/local/lib");

Alternatively add -Djna.library.path=/usr/local/lib/ to your run configuration

@nguyenq
Copy link
Owner

nguyenq commented Feb 13, 2021

Thanks, we've recently confirmed that for setting the system property.

https://sourceforge.net/p/vietocr/discussion/833570/thread/a9998d5d0b/

@chadbrewbaker
Copy link

This works if you are running it under ant on Mac M1.

ANT_OPTS="-Djna.library.path=/opt/homebrew/lib" ant -f mytask.xml

chadbrewbaker added a commit to chadbrewbaker/tess4j that referenced this issue Nov 29, 2022
Load for homebrew MacOS ARM tesseract. Fixes nguyenq#194
@chadbrewbaker chadbrewbaker linked a pull request Nov 29, 2022 that will close this issue
@chadbrewbaker
Copy link

#240 please test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants