NoClassDefFoundError for IndentingXMLStreamWriter class in stax-utils.jar - java

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.

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.

ClassNotFoundException/NoDefFound on Runtime, but class imported works on it's own?

I'm trying to import a jar that I created to my project in Eclipse. When trying any calls from the so-called jar, I get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
I believe this is caused due to the class being present on compile, but not being executed in runtime.
The problem is: When I run the jar on its own (without being imported), it works as intended, with all the functionalities. But I now need it to function as being part of a bigger jar, without changing the runtime classpath (didn't do that to the original project).
How can that be achieved?
#Edit
The class not being found or def not being found is ByteList, from the FastUtil library.
Exception in thread "main" java.lang.NoClassDefFoundError: it/unimi/dsi/fastutil/bytes/ByteList
It's imported TO another Jar, let's call it MyJar.jar; MyJar.jar functions perfectly as a standalone jar, as the intention for it is to be a library, and it functions by itself.
The situation is: When I import MyJar.jar to another project, it won't work anymore, I can't use it's methods and classes inside the new project.
MyJar.jar can be run from the command prompt, with java -jar MyJar.jar, and it will print it's test-page to show that it's working.
The new project compiles, but when it's ran, I get the class not found exceptions regarding MyJar project.
You can use Maven Assembly Plugin to add all classes from related JARs, otherwise you cannot load classes without adding them to classpath.
If you don't want to use Maven then add all needed classes manually to your JAR.

Class not found even when in Jar in the classpath

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...

Getting ClassNotFound Exception for class MkActivityMethod

I am trying to use JackRabbit lbrary for SVN chekin operation.
What I run my code through individual program this works fine. But when I run using web based project it doesn't work.
Code compiles fine but gives me runtime exception at following line :
MkActivityMethod activityMethod = new MkActivityMethod(url);
The exception is :
java.lang.NoClassDefFoundError: org/apache/jackrabbit/webdav/client/methods/MkActivityMethod
My project has jackrabbit-standalone-2.6.4.jar included in my eclipse jars as well as in project web-Inf/lib folder
Please let me know where I am going wrong.
There is obviously something wrong with your classpath. What Web Server Are You Using?
Here is my solution :
Try to build the project once again
Check the JDK version for the builder and Server JRE
Try clean the project (In eclipse , Project menu -> Clean )
Reason :
After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program. The NoClassDefFoundError indicates that the classloader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you're trying to use. It probably indicates that you haven't set the classpath option when executing your code. This link explains how to set the classpath when you execute.

UnsatisfiedLinkError: The library loads but I still get a link error

I have two Eclipse plugins:
plugin-1: provides a package in .jar to other plugins. (a Java Wrapper for a C++ library) This plugin was created by clicking File->New->Other->Plug-in from Existing JAR Archives.
plugin-2: has the native library .so for plugin-1 (Bundle-NativeCode directive is in MANIFEST.MF) and instantiates a class from plugin-1
(I actually tried putting the .so in plugin-1, but I cannot seem to load the library, even with the Bundle-NativeCode directive in the plugin-1 MANIFEST.MF, outside of the plugin project that contains the .so, so I guess I have to bundle the .so with any plugin that uses plugin-1.)
I am running a JUnit tests from plugin-2 which instantiates MyClass from plugin-2 which, in turn, instantiates MyLibraryClass from plugin-1. MyClass successfully loads the native library and instantiates MyLibraryClass without an UnsatisfiedLinkError or other exception being thrown from either the loading of the native library or from instantiating MyLibraryClass. I am not running a plugin in this case -- just the JUnit tests.
When I run plugin-2 (using a product configuration) and instantiate MyClass, the native library loads fine but I get an UnsatisifiedLinkError when MyClass instantiates MyLibraryClass. In this case, I believe the library is being loaded based on the output I get from using the class described in the posting How do I get a list of JNI libraries which are loaded?
NOTE: I'm using Eclipse 3.6.1.
Here is a code sample that shows the essence of what I'm trying to do:
package com.mylibrary;
import com.external_library.MyLibraryClass;
public class MyClass {
public static void loadLibrary() {
// Without Bundle-NativeCode in MANIFEST.MF I get
// "java.lang.UnsatisfiedLinkError: no mylibrary_java in java.library.path"
System.loadLibrary("mylibrary_java"); // Loads libmylibrary_java.so.
// Works fine from JUnit Test
// When I run the plugin, I get an UnsatisfiedLinkError:
// "java.lang.UnsatisfiedLinkError:
// com.external_library.MyLibrary_javaJNI.new_MyLibraryClass__SWIG_3()J"
MyLibraryClass instance = new MyLibraryClass();
}
}
I have replicated your setup and I get the same exception.
The problem could be solved by:
add the native library to plugin-1
add the Bundle-NativeCode directive to plugin-1's Manifest
load the library in the static constructor of plugins-1's Activator (you can write one and add it to the plugin)
Some other possible sources of errors:
Be aware that the package path, the class name and the method signatures should never be changed for any class with native bindings. Otherwise JNI would not be able to find the native counterpart and you get an UnsatisfiedLinkError. In your import directive you specified the following classname com.external_library.MyLibraryClass, but your error message has a different classname com.external_library.MyLibrary_javaJNI. Check for these sources of errors.
Some additional explanations:
A JUnit test in contrast to an JUnit plugin test starts no OSGi environment. Therefore you have a plain Java application with an ordinary JUnit test. If your native lib and your application are contained in the same folder (top level) the native lib will be automatically found on windows. If that is also true on UNIX systems, this would be an explanation why your JUnit test is successful. If it lies in a different folder, you have to specify the Java Library Path for an ordinary Java application.
EDIT by MrMas:
Modify plugin-2 so it doesn't depend on plugin-1 by adding the .jar file to plugin-2.
Copy the .jar file into plugin-2. I put it in the same directory as the .so.
Add the jar to the project via: Project->Properties->Libraries->Add Jar
Add the jar to the class path via plugin.xml->Runtime->ClassPath section->Add
Export the packages from the Jar (if they're needed by downstream plugins)
Remove the dependence of plugin-1 from the plugin.xml->dependencies tab
Now you can load the library with a System.loadLibrary and use the classes from within the plugin and from another plugin.
I chose not to modify plugin-1 because it was created as a plugin from an existing jar to which I couldn't discover how to add an Activator. I instead chose the path of adding the .jar to plugin-2. See Adding jars to a Eclipse PlugIn for additional discussion.
Bundle-NativeCode is an OSGI-tag. This means only OSGI classloaders are using it. In my case, I had an E4-RCP application. One plugin contained the Java class. The native code, however, I put into a fragment.
When loading and looking for a library, the OSGI classloader has a list of fragments (according to the naming of the structure involved) and examines their Bundle-NativeCode using the class NativeCodeFinder. If one has troubles, try to add breakpoints at the relevant functions. getNativePath() returns the entries as read by the OSGIpart.

Categories