Uploaded image for project: 'JDK'
  1. JDK
  2. 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

    Details

    • Subcomponent:
    • Introduced In Build:
      b175

      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.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                zli Zheng Jun Li
                Reporter:
                fyuan Frank Yuan
              • Votes:
                0 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: