• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to add implementation of JAXB-API to classpath

 
Fernando Sanchez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a beginner at JAXB.

I don't know how to add the JAXB-API implementation that this code from a textbook needs (it is excuted without problems from NeatBeans), resulting in this error:

Exception in thread "main" javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
- with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory]
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:278)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:421)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at clasesjaxb.Ejemplo1_JAXB.main(Ejemplo1_JAXB.java:28)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:122)
at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:155)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:276)
... 4 more



The classes in the project compile because I added to the Eclipse classpath jaxb-api.jar file (extracted from jaxb-ri-2.3.1.zip) but I have no idea how to add an implementation for that api.
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where did you get the JAXB RI zip from?

The problem is that you only added the API library to the class path, but not the implementation. Since I don't use Eclipse, I don't know if it allows you to separately specify which dependencies go on the classpath only at runtime, but if it does then there's where you would place the implementation library.

Issues with dependencies are MUCH easier to solve if you use Maven for your projects.
 
Fernando Sanchez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Where did you get the JAXB RI zip from?

The problem is that you only added the API library to the class path, but not the implementation. Since I don't use Eclipse, I don't know if it allows you to separately specify which dependencies go on the classpath only at runtime, but if it does then there's where you would place the implementation library.

Issues with dependencies are MUCH easier to solve if you use Maven for your projects.



So, my situation is:
1. Only api I've found is jaxb-api.jar file (extracted from jaxb-ri-2.3.1.zip) which allows me to compile
2. Only implementation I've found eclipselink.jar (extracted from eclipselink-3.0.3.v202208190922-471432269c) allows me to run
3. The implementation and the api are incomptible



No clue what I could do.


 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to find the version of EclipseLink that is compatible with your version of JAXB.

Again, from where did you get the JAXB RI library from? And why did you choose that version?
 
Tim Holloway
Saloon Keeper
Posts: 28660
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Where did you get the JAXB RI zip from?

The problem is that you only added the API library to the class path, but not the implementation. Since I don't use Eclipse, I don't know if it allows you to separately specify which dependencies go on the classpath only at runtime, but if it does then there's where you would place the implementation library.

Issues with dependencies are MUCH easier to solve if you use Maven for your projects.


Every Eclipse project can, if you wish, be configured to use whichever JDK installed in your system that you desire. You can add your own libraries and class directories into that project classpath as you wish,  And, in fact, you can define such items symbolically to make it easier and more portable between projects and/or other people's machines.

Beyond that, for testing and execution, each run profile that you define for a project can have its own classpath characteristics.

So definitely there's flexibility there.

As a further feature, if your project is defined to Eclipse as a Maven project. Eclipse can extend the project's classpath to reflect the Maven POM dependencies. So all can work in harmony.
 
Fernando Sanchez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You need to find the version of EclipseLink that is compatible with your version of JAXB.



I downloaded the eclipselink-3.0.3.v202208190922-471432269c.zip file from Eclipse server: https://www.eclipse.org/eclipselink/#download and tried adding all the jar files (about 20) to my project CLASSPATH and also added a file called jaxb.properties in the same package as the domain classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory



as explained here https://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/SpecifyRuntime

But the imports cannot be resolved:



Stephan van Hulst wrote:Again, from where did you get the JAXB RI library from? And why did you choose that version?


I can't remember where I downloaded it from, maybe from here: https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-ri/2.3.1 or maybe from here https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/2.3.1/  
 
Fernando Sanchez
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I've found the jaxb-runtime.jar compatible with jaxb-api.jar withing the same jaxb-ri-2.3.1.zip file.

Now I'm getting a different error when the code tries this:


Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/istack/Pool
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1156)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:165)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:577)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:297)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:286)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:409)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:721)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:662)
at classjaxb.Ejemplo1_JAXB.main(Ejemplo1_JAXB.java:29)
Caused by: java.lang.ClassNotFoundException: com.sun.istack.Pool
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 10 more



However with NetBeans everything seems to work ok:
netbeans_ok.png
[Thumbnail for netbeans_ok.png]
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a reason you're using version 2.3 of the JAXB API? As far as I can tell, you're not using a Java EE 8 or older web application container, so you should be able to use the JAXB API version 3.0 without problems.

I got it to work in Eclipse, without Maven. Here's what I did:

  • Download the JAXB RI 3.0.2 standalone ZIP bundle.
  • Unpack the ZIP.
  • Add a new User Library in Eclipse. I named it "JAXB RI 3.0.2".
  • Add all JARs from the mod folder of the unpacked ZIP to the user library, except jaxb-jxc.jar and jaxb-xjc.jar.
  • Under Libraries of the project's Build Path, add the "JAXB RI 3.0.2" user library to the Classpath.
  • Run the application.

  • Here's the code I tested it with:



    And here is the output:
     
    Fernando Sanchez
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the answer. I tried to follow exactly the same steps you tried but I can't get the code compiled:

    1. Under Libraries of the project's Build Path, I added the "JAXB RI 3.0.2" user library to the Classpath
    2. I added that that user library to the project Classpath
    user_classpath.png
    [Thumbnail for user_classpath.png]
    classpath.png
    [Thumbnail for classpath.png]
    imports_ko.png
    [Thumbnail for imports_ko.png]
     
    Stephan van Hulst
    Bartender
    Posts: 15741
    368
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, you need to change the package names from javax to jakarta.

    Another thing, please don't quote entire posts, it makes the topic harder to read. If you want to reply to something specific, please quote only that part of the post.
     
    Fernando Sanchez
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the answer, it finally worked fine. Sorry for quoting the whole previous message, but I coudn't find a button which specifically answered to the previous message in the thread and thought quoting was the only way to make it clear what I was answering too.

    One last thing I wonder: why in the NetBeanst it isn't necessary to add this library whereas in Eclipse it is.
     
    Stephan van Hulst
    Bartender
    Posts: 15741
    368
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    What version of Java are you using in NetBeans?
     
    Fernando Sanchez
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:What version of Java are you using in NetBeans?



    It seems to be Java 1.8, I guess that's the default version (I had never used NetBeans before, contrary to Eclipse which I used to use when I worked in the software industry about 20 years ago).

    Thanks again for your help.
    netbeans.png
    [Thumbnail for netbeans.png]
     
    Stephan van Hulst
    Bartender
    Posts: 15741
    368
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    JAXB was removed from the JDK with Java 11. So if you're working with a Java version older than 11, you don't need to include JAXB through an external library.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic