Class not found even when in Jar in the classpath - java

I am trying to deploy an application that is using Jackson, JUnit, and Commons-IO. I have the following Jars in my application's classpath:
commons-io-2.4.jar
jackson-databind-2.7.0.jar
jackson-annotations-2.7.0.jar
log4j-api-2.4.1.jar
wsdiscovery-0.2.jar
jackson-core-2.7.0.jar
log4j-core-2.4.1.jar
This application works within my development environment, and I have deployed all of the above Jars with the main application jar. I can run the application without problems, but every time I try to use it I get the following failure:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
at com.oncam.hware.app.OnvifApp.formatOutput(OnvifApp.java:356)
at com.oncam.hware.app.OnvifApp.dispatchCommand(OnvifApp.java:271)
at com.oncam.hware.app.OnvifApp.loopSocket(OnvifApp.java:130)
at com.oncam.hware.app.OnvifApp.useSocket(OnvifApp.java:216)
at com.oncam.hware.app.OnvifApp.main(OnvifApp.java:473)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.databind.ObjectMapper
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 5 more
The ObjectMapper class is in the jackson-databind-2.7.0.jar file. Furthermore, I have no problems accessing the classes in the other jar files (including the JUnit jars!). For some reason, it is as if the classloader is loading every Jar except jackson-databind-2.7.0.jar.
Does anyone know what is causing this and how I can fix it?
Someone please advise...

I figured out what was wrong.
It turns out that the environment I am using (Eclipse!) does not properly update the manifest file when you export your code to a JAR file. Without the proper manifest entries, the application cannot "find" the dependent jar files.
This is, in my opinion, a serious oversight on the part of the Eclipse folks -- especially when you have an application that depends on a lot of jar files. In order to make my application run, I had the following choices:
Create a script that runs the jvm and has a list of parameters pointing to every needed jar file, or:
2: Manually enter each required jar file into the Manifest file
To my knowledge, there is no way to automatically update the manifest file. This is a serious PITA (Pain In The A**)...
Anyway, sorry for bothering people about this problem. Hopefully, posting this answer will help others avoid similar problems...

Related

NoClassDefFoundError when running Jar

I have this Problem. When i run my code in Intellij it works fine, but if i do an artifact and build the jar, it doesnt work. I think its caused by an extern library. Heres my output:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mindfusion/scheduling/Calendar
at GUI.<init>(GUI.java:75)
at Logfiles.main(Logfiles.java:13)
Caused by: java.lang.ClassNotFoundException: com.mindfusion.scheduling.Calendar
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 2 more
I know which Class it is but i dont know how to solve the Problem. Im really just a beginner. Could you please help me and explain it simple. Thank you
Edit:
After i build the artifact with extracted Libraries this Error comes : Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
This error simply means the class file is not present in the jar.
One possible solution is you can download jd-gui which is used to look at jars. You can use this to check if the class is present.
Another solution is you can grep search the class in the jar with this simple command.
grep -l "<class-name>" <jar-name>.jar
if the class is not present in the jar file. you can add the class using jar command.
jar -cvf <jar-absolute-location> <class-path>
eg : jar -cvf GUI.jar com.mindfusion.scheduling.Calendar
The easiest way to understand this issue, it to read the Javadoc for that class. From the Javadoc:
Thrown if the Java Virtual Machine or a ClassLoader instance tries to
load in the definition of a class (as part of a normal method call or
as part of creating a new instance using the new expression) and no
definition of the class could be found.
The searched-for class definition existed when the currently executing class was compiled,
but the definition can no longer be found.
That means that NoClassDefFoundError can be thrown when that particular class is present during compile time but somehow not available during runtime. This could be due to missing JAR file, permission issues, or incorrect classpath on runtime.
Normally I see these issues when developers neglect to define the classpath for used libraries. They forget that your IDE has its own file defining the classpath (i.e. Eclipse has the .classpath file) so running the application from the IDE works fine (class is present during compile time), but after the application is compiled and the classpath is not defined in the machine hosting the application, NoClassDefFoundError is thrown (class "missing" at runtime).
My suggestion is figured out first if the classpath is correct. More times than none, this is the issue. If the classpath is correct, make sure all permissions are set correctly.

NoClassDefFoundError for IndentingXMLStreamWriter class in stax-utils.jar

I am writing an XML file with StAX parser using XmlStreamEventWriter, the stax-utils.jar is added correctly to the classpath in Eclipse. I wanted to use IndentingXMLStreamWriter class from this jar file but somehow its throwing below exception:
java.lang.NoClassDefFoundError: javanet/staxutils/IndentingXMLStreamWriter
Caused by: java.lang.ClassNotFoundException: javanet.staxutils.IndentingXMLStreamWriter
cannot be found by RCP_PLUGIN_6.20.0.qualifier
The piece of code causing exception is :
XMLEventFactory eventFactory=XMLEventFactory.newInstance();
if(!tmpSettingsXml.exists())
tmpSettingsXml.createNewFile();
XMLStreamWriter xmlStreamWriter=new IndentingXMLStreamWriter(XMLOutputFactory.newInstance().
createXMLStreamWriter(new FileOutputStream(tmpSettingsXml)));
xmlStreamWriter.writeStartDocument();
If the method containing 'IndentingXMLStreamWriter' is being called from other class of the tool its throwing exception, however, if this method is called from main() in other class its perfectly working fine and able to find classes inside jar.
Anyone can suggest what's wrong here?
An Eclipse/RCP plug-in runs within an OSGi Runtime, which (almost) completely controls its runtime classpath based on the Plug-in Manifest: the MANIFEST.MF file. You need to add any dependencies that aren't your own sources there, and not directly using the Java Build Path UI. The Java Build Path UI isn't locked out because a Plug-in Project is still a Java Project, just with more stuff.
So remove the jar(s) you added to the Java Build Path, open the MANIFEST.MF file in the Plug-in Manifest Editor, and add the jars to the Classpath section on the Runtime tab.

In java runtime, class not found exception

I am building the jar and I'm using this jar in one the my .war. When I run the program I am getting the below exception. But in that jar file, that particular class is there.
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.itc.zeas.custominputformat.CustomTextInputFormat not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:2195)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getInputFormatClass(JobContextImpl.java:174)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:749)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:341)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:168)
at java.security.AccessController.doPrivileged(Native Method)
at
A quick search turned this up.
Your classpath is broken (which is a very common problem in the Java world).
Depending on how you start your application, you need to revise the argument to -cp, your Class-Path entry in MANIFEST.MF or your disk layout.
Maybe you should post more information? Which tools are you using to develop the program, which parameters when compiling, etc..

Parso Java Library Issue

I am trying to use the Parso Java Library in order to read in a .sas7bdat file. My goal is to convert it to a CSV, and then manipulate it further afterwards. Directions I am following for setting it up are here. I have the parso .jar file imported into Eclipse successfully. However, when I try to instantiate as so:
InputStream streamIn = new FileInputStream(sasFile);
SasFileReader sasFileReader = new SasFileReader(streamIn);
I get this exception:
Exception in thread "main"
java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory
at com.ggasoftware.parso.SasFileReader.<clinit>(SasFileReader.java:30)
at sas7bdatFileConverter.convert(sas7bdatFileConverter.java:25)
at sas7bdatFileConverter.main(sas7bdatFileConverter.java:11)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
I would greatly appreciate any hints as to what I might be missing here. Thank you!
The page you cited says you should be using a Maven dependency (which will automatically download all the jar files needed), but it seems like you are directly importing the jar file.
Because you're not using Maven, you also need to include a slf4j jar file (and that's either slf4j-api which doesn't actually do anything, or at least slf4j-simple which logs to the console).
Simply go to http://www.slf4j.org/ and download the newest package, then take out slf4j-api-<version>.jar (and slf4j-simple-<version>.jar if you want to see the Parso library logging into your console).
In the long run, definitely learn how to use Maven, it will immensely ease your pain with library management.

When executing JAR, get ExceptionInInitializerError: version.properties not found

I've been writing a small project in Eclipse which runs perfectly within the IDE. Then I've build a runnable .jar file through Eclipse (which should include every dependency library inside the jar itself).
I use 3 library in my project:
derby.jar
qtjambi-4.7.1.jar
qtjambi-win32-msvc2008-4.7.1.jar
Then I use this command (in windows):
java -jar prova.jar
And I get this:
Connected to database
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:58)
Caused by: java.lang.ExceptionInInitializerError: version.properties not found!
at com.trolltech.qt.Utilities.<clinit>(Unknown Source)
at com.trolltech.qt.QtJambi_LibraryInitializer.<clinit>(Unknown Source)
at com.trolltech.qt.QtJambiObject.<clinit>(Unknown Source)
at WAAAGH.main(WAAAGH.java:52)
... 5 more
As you can see the derby.jar is working as expected ("Connected to database"), but there's an error with Qt-Jambi that I can't understand. Any idea?
EDIT: WAAAGH is the class containing the main method, line 52 consists in:
QApplication.initialize(args);
How is QtJambiObject getting loaded? Have you packeged it inside your prova.jar? The missing file version.properties should be part of the same jar at top level (not in any subdir). It seems you have not packaged it inside prova.jar at top level. See this for explanation of how it is loaded.
You might be better off specifying all jars and main class on command line:
java -classpath prova.jar;derby.jar;qtjambi-4.7.1.jar;qtjambi-win32-msvc2008-4.7.1.jar <your main class>
replace ; with : if you are running on *nix
FWIW the location of version.properties has recently been changed to be inside the package namespace of the bundles com/trolltech/qt/version.properties. The old location was a poor design choice and that has now been corrected. The issue is that if you have another JAR in your classpath that also has a toplevel file then the ClassLoader is entitled to think that the JAR with that file is authorative for the package and it does not need to search another JAR for the file. A package is a minimum deployable unit in Java, only specialist ClassLoaders (such as those use in OSGi) have features to work around this part of the Java design.
Usually your toplevel (application JAR) will be first in the list and I bet in that JAR you have one or more files like /log4j.properties /commons-logging.properties etc... it is because one or more file exists it then masks (hides) the file in the qtjambi-X.Y.Z.jar from being seen at runtime. Which is why the problem might not exist when you test a certain scenario but then appear when you try another (when you changed ClassPath in some way).
My commit to the project at http://qt.gitorious.org/qt-jambi/qtjambi-community/commit/f18ce5da3e30b43424bf94e49adf8c4cac0d9862 better explains in code the very recent change to make life better.
It should never have been the case that you have to copy the version.properties file from the QtJambi redistributable JARs into some other part of the Class Path (like the toplevel project prova.jar in your case) this is a bug that has been corrected for the next release. It is the long term intention to remove the need for the file completely and I am 80% there with that goal, as part of that work making multiple native JARs co-exist in the same Class Path will greatly simplify deployment and getting started guides; as well as making them play with OSGi and Eclipse nicely out-the-box.
However no releases have been made yet to include this change but I am very close (within 30 days of doing so for Qt 4.7.4).
Open Source plug alert: Please consider joining the mailing list at http://lists.qt.nokia.com/pipermail/qt-jambi-interest/ from http://lists.qt.nokia.com/mailman/listinfo for announcements.

Categories