Java exception- Exception in thread “main” java.la

2019-05-14 04:23发布

问题:

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!

回答1:

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.



回答2:

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



回答3:

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.



回答4:

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