10

I am new to java cpp and tesseract-ocr. I am stuck with one issue from couple of hours. I am getting UnsatisfiedLinkError: no jnilept in java.library.path when I create TessBaseAPI. Below is the piece of my code.

public static void tesseractForPdf(String filePath) throws Exception {
    BytePointer outText;

    TessBaseAPI api = new TessBaseAPI();//getting the UnsatisfiedLinkError exception here.
    // Initialize tesseract-ocr with English, without specifying tessdata path
    if (api.Init(".", "ENG") != 0) {
        System.err.println("Could not initialize tesseract.");
        System.exit(1);
    }

    // Open input image with leptonica library
    PIX image = pixRead(filePath);
    api.SetImage(image);
    // Get OCR result
    outText = api.GetUTF8Text();
    String string = outText.getString();
    System.out.println("OCR output:\n" + string);

    // Destroy used object and release memory
    api.End();
    outText.deallocate();
    pixDestroy(image);
}

Exception I am getting on TessBaseAPI api = new TessBaseAPI(); line

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnilept in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:702)
at org.bytedeco.javacpp.Loader.load(Loader.java:500)
at org.bytedeco.javacpp.Loader.load(Loader.java:417)
at org.bytedeco.javacpp.lept.<clinit>(lept.java:10)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.bytedeco.javacpp.Loader.load(Loader.java:472)
at org.bytedeco.javacpp.Loader.load(Loader.java:417)
at org.bytedeco.javacpp.tesseract$TessBaseAPI.<clinit>(tesseract.java:3648)
at om.practiceproblems.BasicTesseractExampleTest.givenTessBaseApi_whenImageOcrd_thenTextDisplayed(BasicTesseractExampleTest.java:35)
at com.practiceproblems.BasicTesseractExampleTest.main(BasicTesseractExampleTest.java:22)
Caused by: java.lang.UnsatisfiedLinkError: no liblept in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:702)
at org.bytedeco.javacpp.Loader.load(Loader.java:491)
... 9 more

I am using java-presets libraries tesseract-3.04.01-1.2 and leptonica-1.73-1.2.jar with javacpp-1.2.1 in my example.I have windows OS. I did see this https://github.com/bytedeco/javacpp-presets/issues/46 and couple of discussions on SO and github which pointed that this issue is fixed in jacacpp-1.1 itself.But I am using javacpp1.2.
I would really appreciate any help in resolving the issue or finding the root cause.

Viraj Nalawade
  • 3,137
  • 3
  • 28
  • 44
  • Follow these instructions and let me know what Dependency Walker says: https://github.com/bytedeco/javacpp-presets/wiki/Debugging-UnsatisfiedLinkError-on-Windows – Samuel Audet Sep 11 '16 at 13:47
  • Thanks for the reply @SamuelAudet.. I tried the steps listed on the given link..I used _Loader.load(tesseract.class)_ in my program as I got exception on creation of _TessBaseAPI_ and as expected it results in UnsatisfiedLinkError.. but I get an error with dependency walker saying 'tempdirectorypath/jnitesseract.dll' was not found.. As mentioned i used Loader.getTempDir() + "jnitesseract.dll" but still had same error.. I also tried java.library.path ie PATH environment variable in windows but got the same error.. Can you please suggest me anything more? – Viraj Nalawade Sep 11 '16 at 17:14
  • That simply means not all the JAR files are in your class path. Please put all the JAR files in your class path. – Samuel Audet Sep 12 '16 at 08:36
  • @SamuelAudet thanks a lot.. i added in eclipse VM arguments the jar which were needed to compile and now it works.. Although i am having trouble with languages other than eng.. my maven install says The forked VM terminated without properly saying goodbye on tessbaseapi.init.. but i think wont be part of this question.. thanks again.. – Viraj Nalawade Sep 13 '16 at 13:13
  • also it wud be helpful if you could point me to some doc related to configs in tessdata.. – Viraj Nalawade Sep 13 '16 at 13:19
  • That would be on Tesseract's site: https://github.com/tesseract-ocr/tesseract/wiki/Documentation – Samuel Audet Sep 16 '16 at 09:34

1 Answers1

2

you could clone or download the project:

https://github.com/bytedeco/javacpp-presets#the-cppbuildsh-scripts

then build the modules: JavaCPP Presets for Tesseract and JavaCPP Presets for Leptonica;

(to build the leptonica project you maybe need to install nasm https://www.nasm.us/)

(to build the entire javacpp-presets project you also have to install cmake)

that would create the native libraries:

libjnilept.so and libjnitesseract.so

then you have to specify the jni.library.path

You can do it with:

System.setProperty(JAVA_LIBRARY_PATH, tmpDirName);
/* Optionally add these two lines */
System.setProperty("jna.library.path", tmpDirName);
System.setProperty("jni.library.path", tmpDirName);
final Field fieldSysPath;

fieldSysPath = ClassLoader.class.getDeclaredField(SYS_PATHS);

fieldSysPath.setAccessible(true);
fieldSysPath.set(null, null);

(you could instead specify the -Djava.library.path= on the virtual machine options)

you only have to put the generated files: libjnilept.so and libjnitesseract.so in some folder and set this path for: jni.library.path

<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>tesseract</artifactId>           
    <version>4.0.0-1.4.4</version>            
</dependency>

<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>leptonica</artifactId>
    <version>1.77.0-1.4.4</version> 
</dependency>

you can also try to add

<dependency>    
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>leptonica-platform</artifactId> 
    <version>1.77.0-1.4.4</version>
</dependency> 

<dependency>
    <groupId>org.bytedeco.javacpp-presets</groupId>
    <artifactId>tesseract-platform</artifactId>
    <version>4.0.0-1.4.4</version>
</dependency>

and add into the build a maven-assembly-plugin

<build>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                         mainClass>fully.qualified.MainClass</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <!-- new -->
            <executions>
                <execution>
                    <id>make-assembly</id> 
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal> 
                    </goals>
                </execution>
            </executions>
        </plugin>   
</build>

 


 

Also, you could also get an error like this:

sscanf(line, "%" QUOTED_TOKENSIZE "s %" QUOTED_TOKENSIZE "s %f %f",
linear_token, essential_token, &ParamDesc[i].Min, &ParamDesc[i].Max) == 4
:Error:Assert failed:in file clusttool.cpp, line 73
#
# A fatal error has been detected by the Java Runtime Environment:

Due to Tesseract's locale requirements, export LC_ALL=C is required before running any client programs.

so:

       <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration> 
                <environmentVariables>  
                    <LC_ALL>C</LC_ALL>
                </environmentVariables>
                <executable>java</executable>
                <arguments>
                    <argument>-classpath</argument>
                    <classpath />
                    <argument>${classpath}</argument>
                </arguments>
            </configuration>
        </plugin> 

source:
    - https://github.com/nguyenq/tess4j/issues/106
    - https://github.com/sirfz/tesserocr/issues/165

ProgrammersBlock
  • 5,974
  • 4
  • 17
  • 21
wiggin200
  • 310
  • 1
  • 4
  • 11
  • In your case you maybe only need to build, JavaCPP Presets for Leptonica; but a have to generate both: libjnilept.so and libjnitesseract.so. – wiggin200 Jan 26 '19 at 17:21
  • possibly true. Although this project had a fallout and I need to check by if this solution works – Viraj Nalawade Jan 27 '19 at 12:03