jakartaee / jaxb-api

BSD 3-Clause "New" or "Revised" License
61 stars 42 forks source link

Invalid JAXB default factory class on JavaSE 11 #78

Closed bravehorsie closed 5 years ago

bravehorsie commented 6 years ago
    private static final String PLATFORM_DEFAULT_FACTORY_CLASS = "com.sun.xml.internal.bind.v2.ContextFactory";

Jaxb api uses invalid default factory class which referes to JDK versions, which has included JAXB implementation. Since JDK 11 it is no longer valid. Default factory class should be "com.sun.xml.bind.v2.ContextFactory" without "internal" so it takes jaxb-ri as default or should be excluded completely printing describing error message when no implementation is found.

See javax.xml.bind.ContextFinder

lukasj commented 4 years ago

version 2.4.0-b180830.0359 has different license - may or may not be an issue.

in any case ballot for 2.3.3 has already been started, see https://www.eclipse.org/lists/jakarta.ee-spec/msg00593.html for details

vidhikapoor1990 commented 4 years ago

Hello, In our application we have upgraded to JAVA 11 .After intergrating the JAXB-API(2.3) ,JAXB-Runtime(2.3) and activation(1.1). I am still getting exception : Implementation of JAXB-API has not been found on module path or classpath.

FelixJongleur42 commented 4 years ago

org.glassfish.jaxb* artifacts we're never intended for usage in OSGi environment (this will very likely change rather sooner than later); if you need OSGi, then com.sun.xml.bind:jaxb-osgi is what one needs (it's being consumed by GlassFish - which is OSGi based, or latest eclipselink)

Solved it for me, thank you!

donatelloOo commented 3 years ago

Still having the issue with following dependencies in the classpath running on JDK11 (oracle):

            <dependency>
                <groupId>jakarta.xml.bind</groupId>
                <artifactId>jakarta.xml.bind-api</artifactId>
                <version>2.3.3</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-runtime</artifactId>
                <version>2.3.3</version>
            </dependency>
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.bind.v2.ContextFactory]
    at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:232)
    at javax.xml.bind.ContextFinder.find(ContextFinder.java:375)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
    at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
    [...]
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
  at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
  at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
  at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:230)
  ... 90 more

What do you suggest ?

donatelloOo commented 3 years ago

See my comment here, when current thread classloader is a PlatformClassLoader, the JAXB runtime MUST be in added as a module in order to be available.

https://stackoverflow.com/questions/54632086/java-11-implementation-of-jaxb-api-has-not-been-found-on-module-path-or-classpa/66068044#66068044

bourgesl commented 3 years ago

Here is my solution using JAXB 2.3.3 (on jdk8 to 16) to explicitely define the ContextFactory class:

    /** JAXB implementation 2.3.0 provided in JMCS libraries */
    private static final String JAXB_CONTEXT_FACTORY_IMPLEMENTATION = "com.sun.xml.bind.v2.ContextFactory";

    static {
        // Define the system property to define which JAXB implementation to use:
        System.setProperty("javax.xml.bind.JAXBContextFactory", JAXB_CONTEXT_FACTORY_IMPLEMENTATION);
        System.setProperty(JAXBContext.JAXB_CONTEXT_FACTORY, JAXB_CONTEXT_FACTORY_IMPLEMENTATION);

        if (org.apache.commons.lang.SystemUtils.JAVA_VERSION_FLOAT >= 16.0f) {
            // JDK16 support fix:
            /*
                ERROR [main] com.sun.xml.bind.v2.runtime.reflect.opt.Injector - null
                java.security.PrivilegedActionException: null
                Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
                    at java.base/java.lang.Class.getMethod(Class.java:2195)
                    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:170)
                    at com.sun.xml.bind.v2.runtime.reflect.opt.Injector$3.run(Injector.java:166)
                    at java.base/java.security.AccessController.doPrivileged(AccessController.java:554)
            */
            final String key = "com.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize";
            System.setProperty(key, "true");
            logger.info("Fix JDK-16+ support: version = {} (set {} = true)", 
                    org.apache.commons.lang.SystemUtils.JAVA_VERSION_FLOAT, key);
        }
        logger.info("JAXB ContextFactory: {}", System.getProperty(JAXBContext.JAXB_CONTEXT_FACTORY));
    }