Issue
I have a Java project in Eclipse that uses javax.xml.bind.JAXB classes.
Starting the application from inside Eclipse works perfectly.
However, when I export the project as (runnable) jar file and run it using java -jar myfile.jar
it terminates with a java.lang.NoClassDefFoundError: javax/xml/bin/JAXBException
.
Also playing around with the three options for Library handling in Eclipse Runnable JAR File Specification (extract, package, sub-folder) does not solve the problem - in fact, no libraries are exported in any case.
It seems that the library for JAXB (it seems to be rt.jar) is not considered as required library to be included into the jar file. However, when running the jar file, it is not found nevertheless. I have read that the library must be added to the classpath but this seems strange to me as rt.jar is part of the standard libraries. Is there something special about this library?
Currently, I do not use Maven or something similar for dependency and build management and if possible I want to avoid it for the future. I think, there also must be a way without Maven.
I found several posts here on SO and in Google but was not able to work out a solution for me.
Thank you very much!
Solution
As remarked in the comments, Eclipse probably uses a different Java version than your system (by default). The JAXB API and implementation is not available in JRE 11.
To work on all versions of Java, your best option is:
- Download the JAXB RI distribution. Nowadays I'll choose version 3.0 (which is binary incompatible with the one in Java 8, since it uses
jakarta.xml
instead ofjavax.xml
for the packages name) as in Juliano's answer:
https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/3.0.0/jaxb-ri-3.0.0.zip
- Copy the 4 files
jakarta.activation.jar
,jakarta.xml.bind-api.jar
,jaxb-core.jar
andjaxb-impl.jar
from themod
folder into the library folder of your project (let's saylib
), - Add the 4 libraries to the project's "Build Path",
- Make sure you use JAXB 3.0 throughout your code (the packages of the annotations and classes start with
jakarta.xml
) - Run the application once in Eclipse, so it updates the Run Configuration (or update the classpath of the Run Configuration yourself),
- Export the project to a JAR file.
Among the three export options proposed by Eclipse: "extract required libraries" will create a so-called fat jar (everything in one JAR-file). It works, but it deletes the licence notices in the JAXB jars (so it can not be distributed). "copy required libraries" is your best option, but then you have to move the jar file together with the subfolder. _". "package required libraries" will not work, since jars in a jar are not read by the JVM (unlike JARs in a WAR package).
Edit by the author of the question:
The above worked for me well except that I experienced small differences how the two libraries (javax.xml in Java 8 and jakarta.xml in version 3.0) handle @XmlAttribute
annotations. In javax.xml, I could place an annotation without further arguments on the public getter-method, e.g.
@XmlAttribute
public String getDescription() {
return "";
}
And this worked when the attribute name in the xml file is description
. However, with jakarta.xml I had to add the name of the attribute:
@XmlAttribute(name="description")
public String getDescription() {
return "";
}
Just in the case, that others experience the same problem.
Answered By - Piotr P. Karwasz