11

Hey i am trying to use tess4j for tesseract and having this issue for eclipse on mac osx .

My tesseract is working fine from terminal but trying to run tess4j through tesseract throws me an error .

java.lang.UnsatisfiedLinkError: Unable to load library 'tesseract': Native library (darwin/libtesseract.dylib)

i do have tessetact dylib and its named libtesseract.dylib in my opt/local/lib which i installed using macport .

Thanks for your help

nestrocuation
  • 219
  • 1
  • 4
  • 9

8 Answers8

15

I know it's an old post. I had this problem too recently when I tried to use Tess4J. However, I managed to find a way around it. I've written a post about it http://www.microshell.com/programming/java/performing-optical-character-recognition-in-java/

In short, the problem is because tess4j-2.0.0.jar doesn't include MacOS library. So I just modified the maven cached jar on mine by doing these steps:

  1. cd /Users/user/.m2/repository/net/sourceforge/tess4j/tess4j/2.0.0 (adjust the directory where your tess4j JAR file resides)
  2. mkdir darwin
  3. jar uf tess4j-2.0.0.jar darwin
  4. cp /opt/local/lib/libtesseract.3.dylib darwin/libtesseract.dylib
  5. jar uf tess4j-2.0.0.jar darwin/libtesseract.dylib
  6. jar tf tess4j-2.0.0.jar (to verify that the file is included)

I was then able to run my Java program after I modify the tess4j-2.0.0.jar file. Below is my MacOS version.

user@laptop:~$ uname -a
Darwin Maresas-MacBook-Pro.local 14.3.0 Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
maresa
  • 571
  • 6
  • 15
  • 2
    Thanks, it works like a charm! For those that installed it with `brew install tesseract`, the 4th step should be similar to `cp /usr/local/Cellar/tesseract/4.1.1/lib/libtesseract.4.dylib darwin/libtesseract.dylib` :) – LetsGoBrandon Jan 22 '21 at 13:22
  • This was an EXCELLENT addition to this post, Thank you! You saved me potentially hours of work to figure this out. – Michael Sims Feb 17 '23 at 10:38
  • @maresa I wrote a library based on this answer to make it easy for people to patch their jar file: https://github.com/EasyG0ing1/FixTess4j4Mac – Michael Sims Feb 17 '23 at 20:20
7

@maresa after a few years your comment helped me, thanks a lot :) A bit different paths, maybe it'll help someone:

  1. cd /Users/username/.m2/repository/net/sourceforge/tess4j/tess4j/4.5.4/
  2. mkdir darwin
  3. jar uf tess4j-4.5.4.jar darwin/
  4. brew info tesseract (here you can find path to libtesseract.4.dylib)
  5. cp /usr/local/Cellar/tesseract/4.1.1/lib/libtesseract.4.dylib darwin/libtesseract.dylib
  6. jar uf tess4j-4.5.4.jar darwin/libtesseract.dylib
  7. jar tf tess4j-4.5.4.jar

MacOS Catalina - 10.15.3

Max M.
  • 256
  • 2
  • 4
5

I had a very similar issue with Ghost4j, i.e.

InvocationTargetException: Unable to load library 'gs': Native library (darwin/libgs.dylib) not found in resource path

Instead of modifying jar files, point jna to the appropriate lib path by setting jna.library.path. In Eclipse, you need to set the system property in run configurations - SO answer for this here - https://stackoverflow.com/a/862405/2163229

If you're using Maven exec:

mvn -Djna.library.path=/opt/local/lib/ exec:java -Dexec.mainClass="foo.bar.NativeThingy"

or

export MAVEN_OPTS="-Djna.library.path=/opt/local/lib/" && mvn exec:java -Dexec.mainClass="foo.bar.NativeThingy"

Obviously, set the path to wherever your libs are installed. In my case, I ran $ locate libgs.dylib and found the above path.

References: https://jna.java.net/javadoc/com/sun/jna/NativeLibrary.html

Community
  • 1
  • 1
5

You need install the tesseract lib on your Mac.

brew install tesseract --with-all-languages

Torbilicious
  • 467
  • 1
  • 4
  • 17
Long Nguyen
  • 9,898
  • 5
  • 53
  • 52
0

Make sure you use libtesseract.dylib of Tesseract 3.02 version. Check out this post on Tesseract Forum.

nguyenq
  • 8,212
  • 1
  • 16
  • 16
  • I am using version 3.02.02, and i checked that post out before but it does not work. And i also added the java -Djava.library.path as vm argument . Can 64 bit jvm throw this error – nestrocuation Jan 28 '14 at 02:55
  • 1
    Very possible. Tesseract and Leptonica are 32-bit libraries. Could take quite a bit of work to compile them to 64-bit. – nguyenq Jan 28 '14 at 03:14
0

This is exactly what I was after today, so thanks for the Q&A above. As one additional step beyond what maresa mentioned, I ran into this error after fixing the one you asked about:

java.lang.UnsatisfiedLinkError: dlopen(/var/folders/sq/rh89_ntd7jqdlv9__25zj9dr0000gp/T/jna--913086793/jna8800789057827590119.tmp, 9): Library not loaded: /usr/local/lib/libjpeg.8.dylib
  Referenced from: /usr/local/lib/liblept.4.dylib
  Reason: image not found

So to fix this I needed to set up a symlink for libjpeg.8.dylib:

ln -s /usr/local/Cellar/jpeg/8d/lib/libjpeg.8.dylib /usr/local/lib/libjpeg.8.dylib

Not sure if there is a way to do this without the symlink (i.e. package it in the jar), but I hope this helps anyone else who is looking at this post.

Andrew Neilson
  • 813
  • 6
  • 6
0

Based on maresa's answer above, which works perfectly, I wrote a library that will make it easy to patch the tess4j.jar file with the appropriate C library from tesseract. You can get it here.

Michael Sims
  • 2,360
  • 1
  • 16
  • 29
0

You should to specify the path to .dylib via Djna.library.path vm option.

In my case: -Djna.library.path=/usr/local/lib

Also this topic may be useful for you, because in my case it is need to install tesseract for x86_64 architecture:

Jamy Cake
  • 1
  • 1