Thursday, August 4, 2022

java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject [Solved]

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject error means that your Java program needed a class called org.apache.xmlbeans.XmlObject but JVM is not able to find that in your application's CLASSPATH. You can see the actual cause of this error is "java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject". The most probable reason for this error could be a missing JAR file. In order to solve this error, you need to first find out which JAR file this class belongs. If you look at the error message it's clearly saying that it's from xmlbeans package, it means this class belongs to XMLBeans library.

Sometime it's not obvious in that case I suggest you take advantage of your IDE like Eclipse, Netbeans or IntelliJ IDEA. In Eclipse, you can search for a particular class using Eclipse shortcut Ctrl + T and then enter the name of the class.

You just need to enter a name, no need to enter a fully qualified name which includes package. Eclipse will display all the JAR, which have class files with the same name, but in order to use this feature, your application needs to be setup in Eclipse with all its dependency.

If that's not the case then just Google it and you will find which library this JAR belongs to. Now coming back to our original problem, how to fix java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject error? Simple, just include xmlbeans-2.6.0.jar into your program's CLASSPATH. BTW, there is just one difficulty, how do you choose the right version of JAR? Let's find out in next paragraph.





How to fix java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject Error?

I have already suggested the simple solution of this problem, but let's understand what causes this problem or when do you see "java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject" error in your program. As I said this error comes when your Java program needs XmlObject class from xmlbeans library, but the xmlbeans.jar is not available in CLASSPATH. 

This is very general and low-level statement. Sometimes this make sense and sometimes doesn't? Why? It makes sense when you know that you are using XMLBeans library let's say for parsing XML documents but it doesn't make sense when you are not doing anything like that, for example, you could get " java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject Error" while reading Excel File in Java using Apache POI library. 

Puzzled right? Yes, but you need to understand something called transitive dependency. You are using Apache POI library to read and write Excel file in Java and this library internally uses xmlbeans. So even though you are not referring to any XMLbeans classes in your Java program, your library is internally using it. 

Since you are not using it directly, you won't get any error at compile time because the library you are using is already compiled, but when you run your program, JVM looks for all the binary class files it needs to execute the program. There it doesn't found xmlbeans.jar and throws the following error

Exception in thread "main" java.lang.NoClassDefFoundError:
  org/apache/xmlbeans/XmlObject
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject
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

Now, you know that in order to solve the problem, you need to add the JAR file of XMLBeans library in your program's CLASSPATH, but the question is how do you find the right version on which your actual library is dependent like Apache POI in our case. 

I suggest you leverage Maven for managing dependency, so instead of manually downloading JAR and adding it into the build path of your application, just specify the right dependency in your pom.xml file and let Maven download those JARs automatically. 

Maven will not only download the dependency you specified but any transitive dependency as well. For example, if you specify apache poi dependency in your Maven pom.xml to run your program which reads data from the Excel file then it will also download the xmlbeans-2.60.jar file. This way, you don't need to find the right version of the library on which other library is dependent.

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject [Solution]

In short :

Error :
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject
or
java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject

Solution : 
Add xmlbeans-2.60.jar into your classpath or use Maven to manage dependency.

That's all about how to solve java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject and java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlObject error in Java. 

If you are using XMLBeans directly or indirectly via any other library, all you have to do is download xmlbeans-2.60.jar file and add into your CLASSPATH, but as I suggested its much better to use Maven for managing dependency, it will not only free you from searching and downloading JARs from internet but also download transitive dependency of any open source library you are using. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.