3

I am try to make things works with tess4j (OCR algorithm), and i m using this code:

import java.awt.image.RenderedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;
import net.sourceforge.tess4j.*;

public static void main(String[] args) throws Exception{

    URL imageURL = new URL("http://s4.postimg.org/e75hcme9p/IMG_20130507_190237.jpg");

    RenderedImage img = ImageIO.read(imageURL);
    File outputfile = new File("saved.png");
    ImageIO.write(img, "png", outputfile);

    try {
    Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping

    //   Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping

        String result = instance.doOCR(outputfile);
        System.out.println(result);

    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

When i run it in Eclipse (juno-64bit) it works perfect!

But from the command line i get this Exception:

Exception in thread "main" java.lang.NoClassDefFoundError: net/sourceforge/tess4j/Tesseract
    at SimpleQueueServiceSample.testOCR(SimpleQueueServiceSample.java:73)
    at SimpleQueueServiceSample.main(SimpleQueueServiceSample.java:57)Caused by: java.lang.ClassNotFoundException: net.sourceforge.tess4j.Tesseract
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 2 more

In my build class path, i hame the correct jars files:

tess4j.jar
jai_imageio.jar

After that i export a simple jar file (the jars are sign at 'order and export' in the build path), and run my code:

java -jar manager.jar

Please help!

gran33
  • 12,421
  • 9
  • 48
  • 76
  • You haven't shown us the crucial part: how you're *running* the code. I strongly suspect the jar files aren't in the class path when you're running them. Just because you're *building* against them doesn't mean they're present in the classpath when you *run* the code. – Jon Skeet May 11 '13 at 16:02
  • Just edited... Please read at the bottom of my question. Thanks! – gran33 May 11 '13 at 16:08
  • Yup - see my answer now that we know how you're running it. – Jon Skeet May 11 '13 at 16:10
  • Not an answer, but you could eliminate use of intermediate files by sending the `BufferedImage` to the engine: `String result = instance.doOCR(img);` – nguyenq May 11 '13 at 17:34

4 Answers4

2

Your jar file manifest should include a Class-Path entry:

Class-Path: tess4j.jar jai_imageio.jar

Those jar files should then be placed in the same directory as your jar file at execution time, so they can be loaded appropriately.

At that point, all should be well. But without that entry in the manifest, there's nothing to connect your jar file with the other jar files it depends on.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

you are packaging the file the wrong way, your dependencies are not in the jar

Dima
  • 8,586
  • 4
  • 28
  • 57
  • 1
    This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – acdcjunior May 11 '13 at 16:20
0

where is your Class ? You have missed to write class . Inside class there would be public static void main(String args[]) and to compile Tesseract instance = Tesseract.getInstance();

you will need tess4j.jar in build path.

AtulS
  • 1
0

If your OS is Windows, libtesseract304.dll (used to OCR) is built with VC2013, so you need other system dll. Download here.

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
songgeb
  • 64
  • 4