java.lang.NoClassDefFoundError occures using tess4j without Eclipse - java

I use tess4j library for my java app. I followed instructions and copied liblept168.dll and libtesseract302.dll into my project root folder. When I run my app with Eclipse, it works normally. But when I export it as runnable jar file, it gives me java.lang.NoClassDefFoundError - UnsatisfiedLinkError: The specified module could not be found. But I am sure, that program see this files, because earlier before I added this files it gave me other error, that files can't be found. I also used to copy this dlls to my jar archive, but it didn't help. I am absolutely sure, that I use right jre too. I just can't understand what can be wrong...
Tesseract1 api = new Tesseract1();
api.setTessVariable("tessedit_char_whitelist", "(),-+0123456789");
//part of code of getting image
String result = api.doOCR(image);
Any ideas?
UPD:
I edited my manifest file as you said and it is:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ jsoup-1.7.3.jar json_simple-1.1.jar Filters.jar ja
i_core.jar jna-4.1.0.jar tess4j.jar jai_codec.jar jxl.jar myjsocksj.j
ar jai_imageio.jar
Class-Path: jai_imageio.jar tess4j.jar
Rsrc-Main-Class: com.slando.MainWindow
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
Then there were errors about missing dll files and I copied them into jar. And then the same error began to occure.
Stacktrace:
java.lang.NoClassDefFoundError: Could not initialize class net.sourceforge.tess4j.Tesseract1
at com.slando.parser.ImageParser.getNumberFromImage(ImageParser.java:109)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:138)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:144)
at com.slando.parser.PageParser.loadPhone(PageParser.java:202)
at com.slando.parser.PageParser.loadFullInfo(PageParser.java:319)
at com.slando.HttpThread.run(HttpThread.java:58)
at java.lang.Thread.run(Unknown Source)
One more:
java.lang.UnsatisfiedLinkError: The specified module could not be found.
at com.sun.jna.Native.open(Native Method)
at com.sun.jna.Native.open(Native.java:1759)
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:260)
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)
at com.sun.jna.Native.register(Native.java:1396)
at com.sun.jna.Native.register(Native.java:1156)
at net.sourceforge.tess4j.TessAPI1.<clinit>(Unknown Source)
at com.slando.parser.ImageParser.getNumberFromImage(ImageParser.java:109)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:138)
at com.slando.parser.ImageParser.loadNumberFromImage(ImageParser.java:144)
at com.slando.parser.PageParser.loadPhone(PageParser.java:202)
at com.slando.parser.PageParser.loadFullInfo(PageParser.java:319)
at com.slando.HttpThread.run(HttpThread.java:58)
at java.lang.Thread.run(Unknown Source)

Eclipse has the tess4j jar in its classpath, but your system does not. To distribute this project, the tess4j jar needs to be in the classpath in your jar manifest. You can also add the path to this jar to the classpath environment variable in your system settings.
Someone else had the same problem

Related

CMake add_jar with INCLUDE_JARS not working

I am using CMake to compile jar file with add_jar command. Problem is that when I try to add INCLUDE_JARS to specify dependency to external jar, the code will not run. Here is the code example:
add_jar(testJar
SOURCES
sources/com/test/Main.java
INCLUDE_JARS
${CMAKE_SOURCE_DIR}/extern/org.json/json-20171018.jar
ENTRY_POINT com.test.Main
)
Running the testJar with "java -jar testJar.jar" gives me the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
The org.json jar should be in classpath, right? Adding manifest file with classpath solves the problem but is there way to do this without the manifest file?
And yes, I would use maven or gradle for building but as of restrictions in the project I cannot do that :)
The option INCLUDE_JARS only makes sure that the given external jar is added to the class path upon compilation of the given Java source files. The jar file is neither copied to the build directory nor is it added to the jar file generated by add_jar.
If inclusion of the external jar in the Manifest file is not an option for you, manually add it to the class path upon running the testJar, i.e.:
java -cp path/to/json-20171018.jar -jar testJar.jar

Properly create Java JAR

I am using IntelliJ to create a variant of the Java WorldWind application. When I build the JAR artifact in IntelliJ I can see my DLLs and SOs in the top-level directory of the JAR. However, when I try to execute it, it complains it can't see these libraries that are inside the JAR (tried both Windows and Linux, thus being why I include SOs and DLLs).
What do I need to do to get it to see these libraries?
This is my first time creating an executable JAR, so sorry for the vagueness, I'm not quite sure what information is important. Please ask me any questions necessary to better understand this problem, and I will answer them.
Edit:
Catched FileNotFoundException: D:\code\Animator\animator\out\artifacts\Animator_jar\Animator-natives-windows-i586.jar (The system cannot find the file specified),
while addNativeJarLibsImpl(
classFromJavaJar class com.jogamp.common.os.Platform,
classJarURI jar:file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator.jar!/com/jogamp/common/os/Platform.class,
nativeJarBaseName Animator-natives-windows-i586.jar):
[ file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator.jar ->
file:/D:/code/Animator/animator/out/artifacts/Animator_jar/ ] +
Animator-natives-windows-i586.jar ->
slim: jar:file:/D:/code/Animator/animator/out/artifacts/Animator_jar/Animator-natives-windows-i586.jar!/
Exception in thread "main" java.lang.UnsatisfiedLinkError: Can't load library: D:\code\Animator\animator\out\artifacts\Animator_jar\gluegen-rt.dll
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibraryInternal(JNILibLoaderBase.java:551)
at com.jogamp.common.jvm.JNILibLoaderBase.access$000(JNILibLoaderBase.java:64)
at com.jogamp.common.jvm.JNILibLoaderBase$DefaultAction.loadLibrary(JNILibLoaderBase.java:96)
at com.jogamp.common.jvm.JNILibLoaderBase.loadLibrary(JNILibLoaderBase.java:414)
at com.jogamp.common.os.DynamicLibraryBundle$GlueJNILibLoader.loadLibrary(DynamicLibraryBundle.java:388)
at com.jogamp.common.os.Platform$1.run(Platform.java:209)
at java.security.AccessController.doPrivileged(Native Method)
at com.jogamp.common.os.Platform.<clinit>(Platform.java:179)
at javax.media.opengl.GLProfile.<clinit>(GLProfile.java:82)
at au.gov.ga.worldwind.animator.application.AnimatorConfiguration.<clinit>(AnimatorConfiguration.java:46)
at au.gov.ga.worldwind.animator.application.Animator.<clinit>(Animator.java:205)
As far as the structure of the JAR, I don't know how to show other than a screenshot of the JAR in 7-Zip:
In the image we can see that the name of the jar is: Animator.jar, but intelliJ is searching for: Animator-natives-windows-i586.jar. You must go to Module Settings - Artifacts and change the OutputDirectory to: D:\code\Animator\animator\out\artifacts\Animator_jar
Then check the name of the artifact to match the path and the name of the file.
The image added by me is an example.

Problem when creating Jar file using Command Line

I have tried to create Jar file using Command Line.
My Manifest file:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class:Home
My Files and Location of that files:
Location : D:\Application
Files :
images
add.png
home.png
minus.png
Database.java
Home.java
UiDesign.java
Database.class
Home.class// This is my main class
UiDesign.class
Manifest.txt
mysql-connector-java-5.1.15-bin.jar
To create jar file i tried:
D:\Application>jar cmf Manifest.txt MyApp.jar *.class mysql-connector-java-5.1.15-bin.jar images
But the jar file is created. If i click that jar file, Error message shows like below,
Failed to Load Main-Class manifest attribute from
D:\Application\MyApp.jar
Thank for all....My problem solved by adding new line in Manifest File....Thank you all...
But now i have another problem.....
D:\JavaApplication-13-8-2011\Application>jar cfm MyApp.jar Manifest.txt *.class
mysql-connector-java-5.1.15-bin.jar images
D:\JavaApplication-13-8-2011\Application>java -jar MyApp.jar
Connect to MySQl
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database.getDBConnection(Database.java:14)
at UiDesign.<init>(UiDesign.java:58)
at Home.main(Home.java:6)
java.lang.NullPointerException
at Database.getBrand(Database.java:31)
at UiDesign.<init>(UiDesign.java:59)
at Home.main(Home.java:6)
Exception in thread "main" java.lang.NullPointerException
at UiDesign.<init>(UiDesign.java:64)
at Home.main(Home.java:6)
D:\JavaApplication-13-8-2011\Application>
I think this problem occurs due to to class path not set to mysql-connector....I have added this jar file in my Application.....How to setclass path and run my Application jar file successfully.....
please help me....
Thank you...I cleared..this problem also...As Trisstan said, I added classpath in Manifest file...Now My Application Jar file Run Succesfully.....
Thank You all for your Quick Response......
Add a newline at the end of your manifest file.
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
From: http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
It may be an error about classpath, because your jar depends on mysql-connector-java-5.1.15-bin.jar
make sure there is a carriage return in your manifest file and I am also guessing your jar file name should precede the manifest file as your option says cfm.
Failed to Load Main-Class manifest attribute from D:\Application\MyApp.jar
From the error it looks like it thinks that the jar file is your manifest file. That should be the problem.
update the argument or change the option parameter to mcf I guess..
For your second problem:
Your jar probably contains a copy of the mysql jar. That does not work because jars are supposed to contain classes. The simplest way to fix this is to indicate in your manifest that your code depends on the mysql jar. Basically add the following:
Class-Path: mysql-connector-java-5.1.15-bin.jar
In your jar creation command, do not include the mysql jar. Finally, make sure that your jar and the mysql one are distributed together (in the same directory).
If you really want a single jar, extract the mysql jar and include its contents in the jar you are going to distribute.

Cannot find class even when jar file is in working directory

I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jar below. I kept on getting a class not found error, until finally I put all the external libraries in the same directory.
$ ls
JAXB2_20081030.jar
JAXB2_20110601.jar
activation.jar
asjava.jar
commons-beanutils-1.8.3.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.5.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-2.1.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-logging-adapters-1.1.1.jar
commons-logging-api-1.1.1.jar
jRams.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jaxb1-impl.jar
jremote.jar
jsr173_1.0_api.jar
log4j-1.2.16.jar
netty-3.2.4.Final.jar
$
Still, I get the class not found error.
$ java -jar jRams.jar
The java class is not found: com.jbase.jremote.JRemoteException
jremote.jar definitely contains JRemoteException. Why isn't this working?
UPDATE
Thank you for your straight-to-the-point answers. I now understand the nature of a java application and a manifest file far better.
Turns out my ftp client was transferring in ASCII mode and not Binary, so the jar files were corrupt. I have learned a great deal, nonetheless.
When using the -jar option, you need to specify which jar-files should be on your class path in the manifest file. Just having the required jar files in the same directory won't do it.
Add a line in your manifest that says:
Class-Path: JAXB2_20081030.jar:JAXB2_20110601.jar:....:netty-3.2.4.Final.jar
or skip the -jar option and launch using
java -cp JAXB2_20081030.jar:....:netty-3.2.4.Final.jar:jRams.jar pkg.JRamsMain
and it should work fine.
(Note that on *nix systems, as opposed to Windows machines, the jar files in the class paths should be separated using : instead of ;.)
Further reading:
The Java Tutorials: Adding Classes to the JAR File's Classpath
You need to add all those JARs to the runtime CLASSPATH by adding the -classpath parameter. AIX requires you to separate the JARs using :
You will have to specify the full path(if libraries not in the same directory as jRams) or just the names of the jar file in a manifest file (If all dependency jars are in the same folder). Alternative specify the path to all the dependent jars using -cp argument.
Example (This assume every dependency is in the same directory you are executing java command from):
java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java
Where package_to_class is example: com.myproj.example.
EDITED.
Follow these steps to add "Class-Path" to existing jar file -
Create "newmanifest" file with following entry
Class-Path: additional/jars
Update existing jar file e.g. "classes.jar"
jar --update --manifest=newmanifest --file classes.jar
Inflate jar file
jar -xvf classes.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
Verify "Class-Path" is added to MANIFEST.MF
cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
main-class: CLASSNAME
Created-By: 15.0.2 (Oracle Corporation)
Class-Path: additional/jars

Use Bundle-NativeCode on Linux does not work

I create a plugin which includes the following folder structure:
src
native/so/libsystemcommand.so
META-INF/MANIFEST.MF
The manifest include the command
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Commands Plug-in
Bundle-SymbolicName: de.system.commands;singleton:=true
Bundle-Version: 1.0.0
Bundle-Activator: de.system.commands.CommandsPlugin
Bundle-Localization: plugin
Bundle-NativeCode: native/so/libsystemcommand.so; osname = Linux; processor = x86
Require-Bundle: org.eclipse.core.runtime,
org.apache.commons.logging
Eclipse-AutoStart: true
Export-Package: de.system.commands,
de.system.commands.jni,
de.system.commands.utils
Bundle-ClassPath: .
The build.properties looks like
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.,\
native/
In the start method of my Activator class I call
System.loadLibrary("systemcommand");
At runtime the library is not found and a UnsatisfiedLinkError is thrown.
java.lang.UnsatisfiedLinkError: no libsystemcommand in java.library.path
Do I have to set more attributes in the plugin? Do I have to unzip some informations on the target platform?
EDIT:
java.library.path=/opt/jdk/j2re1.4.2_16/lib/i386/client:/opt/jdk/j2re1.4.2_16/lib/i386:/opt/jdk/j2re1.4.2_16/../lib/i386::/opt/dsa/lib:/opt/dsa/lib
I wonder if the library needs to be specified without the lib prefix? E.g.,
System.loadLibrary("systemcommand");
Since that is how the library would be passed on a gcc link line.
The lib has to be in your filesystem (not in an archive file).
Then you can either use the linux environment variable LD_LIBRARY_PATH pointing to the lib or define the property java.library.path
In a plugin fragment for linux, I use:
Bundle-NativeCode: librptlc.so; osname = linux; processor=x86
And in the main plugin I use:
if (OS.equals(Platform.OS_LINUX)) {
System.loadLibrary("rptlc");
}
This should work within one plugin too.
I seem to remember having some problems with libraries in a sub folder in the jar, but I'm not sure why this would be the case. I just stuck to having the libraries in the root of a plugin fragment instead, which works for me.
You could also try getting the file system path of the library (not sure if that's easy) and loading it using:
libraryPath = "C:\eclipse\bundles\123\librptlc.so";
System.load(libraryPath);
I think i found the solution.
We only build the plugin which was not working and copy it to the destination platform directory. After this we start the application as wtach the log files whether the library was foud or not.
What we miss, was to delete the configurations folder. The new plugin was not unzipp and the library was not existing in the configurations directory.
Im sorry and thank you for your answers.
EDIT :
The configuration folder is placed at
<installation>/eclipse/configuration/
At least delete everything without the file
<installation>/eclipse/configuration/config.ini

Categories

Resources