JDK-8184726 : JAXBContext.newInstance(Class) throws "JAXBException: Package java.lang with JAXB class xxx defined in a module java.base must be open to at least java.xml.bind module" if the class is in java.lang package
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxb
  • Affected Version: 9,10
  • Priority: P2
  • Status: Closed
  • Resolution: Won't Fix
  • Submitted: 2017-07-17
  • Updated: 2018-05-02
  • Resolved: 2018-04-20
Related Reports
Relates :  
Relates :  
Sub Tasks
JDK-8185264 :  
Description
Run the following java program:

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;

public class TestJAXB {

    public static void main(String[] args) throws Exception {
        // marshal
        int[] x = new int[]{1,5,2,3,4};
        JAXBElement<int[]> xe = new JAXBElement<int[]>(new QName("", "root"), int[].class, JAXBElement.GlobalScope.class, x);
        Marshaller m = JAXBContext.newInstance(int[].class).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
        m.marshal(xe,System.out);
        
        // unmarshal
        Unmarshaller u = JAXBContext.newInstance(int[].class).createUnmarshaller();
        JAXBElement e = u.unmarshal(new StreamSource(new StringReader(
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" + 
                "<root>\n" + 
                "    <item>1</item>\n" + 
                "    <item>5</item>\n" + 
                "    <item>2</item>\n" + 
                "    <item>3</item>\n" + 
                "    <item>4</item>\n" + 
                "</root>\n")), int[].class);
    }
}

Exception in thread "main" javax.xml.bind.JAXBException: Package java.lang with JAXB class [I defined in a module java.base must be open to at least java.xml.bind module.
	at java.xml.bind@9/javax.xml.bind.ModuleUtil.delegateAddOpensToImplModule(ModuleUtil.java:148)
	at java.xml.bind@9/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:285)
	at java.xml.bind@9/javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:270)
	at java.xml.bind@9/javax.xml.bind.ContextFinder.find(ContextFinder.java:407)
	at java.xml.bind@9/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:706)
	at java.xml.bind@9/javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:647)
	at TestJAXB.main(TestJAXB.java:16)


Please note, the exception is thrown in JAXBContext.newInstance(int[].class), other code is just to show a whole example.
Comments
The Java EE modules have been removed from the JDK as of JDK 11. This issue can now be tracked via : https://github.com/javaee/jaxb-v2/issues/1184
20-04-2018

JAXB demands that all classes specified to newInstance be open to java.xml.bind. In the case, a primitive array is specified to newInstance. Is this type of use-case usual? Asking because JAXB cannot demand that java.lang or any core packages be open to java.xml.bind.
17-07-2017

I suspect the issue is related to JDK-8176508 or JDK-8181087
17-07-2017