0

Currently trying to get JAXB up and running. Dev tool of choice is IntelliJ IDEA.

I found Dariawan quite helpful on this topic, and basically cloned that code into a IDEA/maven project: https://github.com/ldericher/jaxb_experiment.

If I run JAXB_Experiment.java:main() from the IDE, it works and produces the desired output.

However, while mvn clean package completes successfully and a JAR is created, I cannot run the resulting archive:

% java -jar target/jaxb-experiment-1.0.jar
Error: Unable to initialize main class de.rwth_aachen.swc.miehe.experiments.jaxb.JAXB_Experiment
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

Searching for the Error yields tons of advice to add a variety of JAXB dependencies to the pom.xml, of which I have tried most over the course of today, none of which has done me any better.

This is quite unsatisfactory, and I feel like I might be missing something more general.

LDericher
  • 295
  • 1
  • 3
  • 12
  • Is JAXB in your manifest? Post dependencies section from your pom.xml. – MarsAtomic Dec 02 '19 at 23:53
  • It's right there. https://raw.githubusercontent.com/ldericher/jaxb_experiment/master/pom.xml – LDericher Dec 02 '19 at 23:55
  • Does this answer your question? [How to resolve java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException in Java 9](https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j) – MarsAtomic Dec 03 '19 at 00:06
  • That's a site suggesting "to add a variety of JAXB dependencies to the pom.xml, of which I have tried most over the course of today, none of which has done me any better." Excuse my salt. – LDericher Dec 03 '19 at 00:14

2 Answers2

2

IntelliJ adds the projects dependencies to the classpath when running. When running per CLI you have to do the same.

ldz
  • 2,217
  • 16
  • 21
  • I will need this in a plugin, it should be self-contained. Assume I can't control the CLI arguments fed to JVM. – LDericher Dec 03 '19 at 00:01
  • 1
    Then you might want to look into [building a far jar using maven](https://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven). – ldz Dec 03 '19 at 00:05
0

This is quite unsatisfactory1, and I feel like I might be missing something more general.

This is standard Java stuff. A consequence of the way(s) that the java command finds dependencies at runtime.

If you want a JAR that you can run like that (java -jar ...) it will need to be built as a "uberJar" with all of the dependencies embedded in the JAR file itself. There are Maven plugins that will generate an "uberJAR"; e.g. the "shades" plugin.

For more details, read Building a fat jar using maven.

The -jar option tells the JVM to ignore the CLASSPATH environment variable and any -cp options. It is possible to define a classpath in the JAR file's manifest, but that means you are embedding file pathnames in the JAR which leads to other problems.

An alternative is to not use -jar. Instead, write a wrapper script for the java command and have the wrapper specify the application's classpath via a -cp option. The classpath should list all of the dependent JARs, including the JARs containing the JAXB implementation and its dependencies.

With Java 9+, a second alternative is to use jlink to generate an executable consisting of your application, its dependencies and a cut-down JRE.


1 - I could debate your "unsatisfactory" label, but it is pointless. This stuff has been like this for 20 years or more, and is (IMO) not likely to change now. Millions (probably) of programmers have figured out how to work with the Java way of dealing with dependencies, whether it is satisfactory or not.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks, at least that means I didn't really fail, as my Example works as expected in the IDE. I will rephrase my question closer to my actual application. – LDericher Dec 03 '19 at 00:31