1

I have a simple enough program:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

public class Main {

    public static void main(String[] args) throws JAXBException {
        JAXBContext context0 = JAXBContext.newInstance(String.class);
        
    }

}

Because I want to use java-11, javax.xml.bind is no longer provided so I need to add it myself. I do not want to use maven but add external jars. As suggested by this https://stackoverflow.com/a/43574427/3014199 I first add jakarta.xml.bind-api-2.3.2.jar and then its dependency per https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api/2.3.2 jakarta.activation-api-1.2.1.jar to the module path and mark both for export:
enter image description here enter image description here

Now I get

Error: Unable to initialize main class Main
Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

so I add the other library suggested above jaxb-runtime-2.3.2.jar to the classpath: enter image description here

The error does not change so I add all compile dependencies per https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime/2.3.2:

  • FastInfoset-1.2.16.jar
  • istack-commons-runtime-3.0.8.jar
  • stax-ex-1.8.1.jar
  • txw2-2.3.2.jar

to the classpath:
enter image description here
To be sure I again check all jars under "Order and Export", but the error remains. I have also tried adding all jars to module path and adding all jars to class path but to no avail.

peer
  • 4,171
  • 8
  • 42
  • 73
  • Do you have a `module-info.java` file? If yes, please show it. – howlger Jul 08 '20 at 10:06
  • @howlger I don't have a ´module-info.java´ file, I always click on ´Don't Create´. Do I need one? – peer Jul 08 '20 at 10:19
  • You forgot to add the dependencies of those two libraries. They are listed on their info page at mvnrepository.com. If you would use Maven or Gradle such dependencies would have been added automatically. – Robert Jul 08 '20 at 11:15
  • You don't need a `module-info.java` file. But if you would have one, you would have to put the JARs on the modulepath, otherwise like in your case you have to put the JARs on the classpath (modulepath would also work if there is no package that exists in more than one JAR). A JAR that is exported is visible to other projects that have the project in the Java Build Path (if you only have one project, it doesn't matter). Are there errors or warnings in `Main`? If not, look into your run configuration for the JAR containing the missing class (_Dependencies_ tab and _Show Command Line_ button). – howlger Jul 08 '20 at 14:16

1 Answers1

1

After moving all jars to classpath I got a classNotFoundException for com.sun.xml.internal... upon adding the jar at https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl/2.3.1, everything works.

peer
  • 4,171
  • 8
  • 42
  • 73