Problem with JAXBContext.newInstance

Lance Andersen lance.andersen at oracle.com
Tue Feb 12 00:28:48 UTC 2019


The last time I tried, you could not use JAXB 2.4 as a module but that was before it went final.

JAXB has dependencies on JAF so make sure you include that module. If you are still struggling I would follow up on the JAXB dev mailing list https://javaee.groups.io/g/metro for better guidance with JAXB 2.4

The documentation definitely needs some updates  to 7.2.2 as it does not even mention JAF or provide a current of example for using the standalone JAXB as a module without upgrading the JDK implementation.  Also the release notes do not mention the runtime jar https://javaee.github.io/jaxb-v2/doc/user-guide/ch02.html#jars

> On Feb 11, 2019, at 7:03 PM, Oliver Z. <oliver.zemann at gmail.com> wrote:
> 
> Thanks for the documentation link. It says, JPMS is supported when jaxb 2.4.0 comes out. So in that case, this is simply not yet supported, is this correct? For the moment, i can remove the module-info.java, then it works - at least for the moment as workaround, until 2.4.0 is released. 
> I believe, the example below only works because it has no module-info.java
> 
>> Using the JAX-B standalone,https://github.com/eclipse-ee4j/jaxb-ri <https://github.com/eclipse-ee4j/jaxb-ri>, seems a bit harder than it probably should be given where we are. 
>> 
>> The following command line  with the jars on the classpath:
>> ——————
>> java -cp classes/:jars/jaxb-api-2.4.0.jar:jars/jaxb-runtime-2.4.0.jar:jars/jaxb-impl-2.4.0.jar:jars/istack-commons-runtime-3.0.7.jar:jars/javax.activation-api-1.2.0.jar  JAXBExample
>> —————————
>> 
>> will work with the following trivial example:
>> 
>> ———————
>> import javax.xml.bind.annotation.XmlAttribute;
>> import javax.xml.bind.annotation.XmlElement;
>> import javax.xml.bind.annotation.XmlRootElement;
>> 
>> @XmlRootElement
>> public class Customer {
>> 
>>     String name;
>>     int age;
>>     int id;
>> 
>>     public String getName() {
>>         return name;
>>     }
>> 
>>     @XmlElement
>>     public void setName(String name) {
>>         this.name = name;
>>     }
>> 
>>     public int getAge() {
>>         return age;
>>     }
>> 
>>     @XmlElement
>>     public void setAge(int age) {
>>         this.age = age;
>>     }
>> 
>>     public int getId() {
>>         return id;
>>     }
>> 
>>     @XmlAttribute
>>     public void setId(int id) {
>>         this.id = id;
>>     }
>> 
>> }
>> 
>> import java.io.File;
>> import javax.xml.bind.JAXBContext;
>> import javax.xml.bind.JAXBException;
>> import javax.xml.bind.Marshaller;
>> 
>> public class JAXBExample {
>>     public static void main(String[] args) {
>> 
>>         Customer customer = new Customer();
>>         customer.setId(100);
>>         customer.setName("Lance Andersen");
>>         customer.setAge(29);
>> 
>>         try {
>> 
>>             File file = new File("Customer.xml");
>>             JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
>>             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
>> 
>>             // output pretty printed
>>             jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
>> 
>>             jaxbMarshaller.marshal(customer, file);
>>             jaxbMarshaller.marshal(customer, System.out);
>> 
>>         } catch (JAXBException e) {
>>             e.printStackTrace();
>>         }
>> 
>>     }
>> }
>> ——————————
>> 
>> The JAX-B 2.4 documentation could be a bit more helpful https://javaee.github.io/jaxb-v2/doc/user-guide/release-documentation.html#deployment-jaxb-on-jpms <https://javaee.github.io/jaxb-v2/doc/user-guide/release-documentation.html#deployment-jaxb-on-jpms> :-(
>> 
>> 
>> HTH
>> 
>> Lance
>> <oracle_sig_logo.gif> <http://oracle.com/us/design/oracle-email-sig-198324.gif>
>>  <http://oracle.com/us/design/oracle-email-sig-198324.gif> <http://oracle.com/us/design/oracle-email-sig-198324.gif>
>>  <http://oracle.com/us/design/oracle-email-sig-198324.gif>Lance Andersen| Principal Member of Technical Staff | +1.781.442.2037
>> Oracle Java Engineering 
>> 1 Network Drive 
>> Burlington, MA 01803
>> Lance.Andersen at oracle.com <mailto:Lance.Andersen at oracle.com>
>> 
>> 
>> 

 <http://oracle.com/us/design/oracle-email-sig-198324.gif>
 <http://oracle.com/us/design/oracle-email-sig-198324.gif> <http://oracle.com/us/design/oracle-email-sig-198324.gif>
 <http://oracle.com/us/design/oracle-email-sig-198324.gif>Lance Andersen| Principal Member of Technical Staff | +1.781.442.2037
Oracle Java Engineering 
1 Network Drive 
Burlington, MA 01803
Lance.Andersen at oracle.com <mailto:Lance.Andersen at oracle.com>





More information about the jigsaw-dev mailing list