Problem with JAXBContext.newInstance

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with JAXBContext.newInstance

Oliver Z.
Hi, i am trying to create a java 11 application which reads some .xml
and that should be mapped to a object. Unfortunately, with java 11 this
seems impossible. I created a stackoverflow posting. Maybe someone could
take a look at that and help me with my problem.
https://stackoverflow.com/questions/54632086/java-11-implementation-of-jaxb-api-has-not-been-found-on-module-path-or-classpa

Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Lance Andersen
Using the JAX-B standalone,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 :-(


HTH

Lance
> On Feb 11, 2019, at 4:46 PM, Oliver Z. <[hidden email]> wrote:
>
> Hi, i am trying to create a java 11 application which reads some .xml and that should be mapped to a object. Unfortunately, with java 11 this seems impossible. I created a stackoverflow posting. Maybe someone could take a look at that and help me with my problem. https://stackoverflow.com/questions/54632086/java-11-implementation-of-jaxb-api-has-not-been-found-on-module-path-or-classpa
>

 <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
[hidden email] <mailto:[hidden email]>



Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Oliver Z.
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,
> 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 :-(
>
>
> HTH
>
> Lance
> <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
> [hidden email] <mailto:[hidden email]>
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Lance Andersen
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. <[hidden email]> 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
>> [hidden email] <mailto:[hidden email]>
>>
>>
>>

 <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
[hidden email] <mailto:[hidden email]>



Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Oliver Z.
Can you tell me where i can download the JAXB 2.4 final module? I only
found 2.4.0-b... which does not seem to be a final to me.

I also tried to write to that list but i am waiting for approval, which
seems pretty long now (maybe the owner is on vacation or so..). Do you
know any other mailing list/resource i could contact to get that
information? Otherwise i guess i have to go with xstream to not to be
blocked for days now.

Am 12.02.2019 um 01:28 schrieb Lance Andersen:

> 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
>
Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Alan Bateman
On 13/02/2019 11:14, Oliver Z. wrote:
> Can you tell me where i can download the JAXB 2.4 final module? I only
> found 2.4.0-b... which does not seem to be a final to me.
>
> I also tried to write to that list but i am waiting for approval,
> which seems pretty long now (maybe the owner is on vacation or so..).
> Do you know any other mailing list/resource i could contact to get
> that information? Otherwise i guess i have to go with xstream to not
> to be blocked for days now.
I think you need to follow up on the Eclipse or Jakarta EE mailing lists
about this as JAXB is not maintained here. From what I can tell from the
release notes [1],  the Java EE JAXB project released 2.4.0 with changes
to fix the issues they had deploying the JAXB runtime on the module
path. It was a bit awkward before those changes in that you had to leave
the runtime on the class path when running with java.xml.bind on the
module path. I have no insight into why the download on the Eclipse site
is 2.3.2 rather than the latest.

-Alan

[1] https://javaee.github.io/jaxb-v2/doc/user-guide/ch02.html#a-2-4-0
Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Oliver Z.
I found the solution. Pretty trivial, but probably unexpected when
migrating. It is required to have 2 require entries in the
module-info.java. I guess one for the API, and one for the implementation:

requires java.xml.bind;
requires com.sun.xml.bind;

Then it works. Thanks for your effort!

Am 13.02.19 um 12:30 schrieb Alan Bateman:

> On 13/02/2019 11:14, Oliver Z. wrote:
>> Can you tell me where i can download the JAXB 2.4 final module? I
>> only found 2.4.0-b... which does not seem to be a final to me.
>>
>> I also tried to write to that list but i am waiting for approval,
>> which seems pretty long now (maybe the owner is on vacation or so..).
>> Do you know any other mailing list/resource i could contact to get
>> that information? Otherwise i guess i have to go with xstream to not
>> to be blocked for days now.
> I think you need to follow up on the Eclipse or Jakarta EE mailing
> lists about this as JAXB is not maintained here. From what I can tell
> from the release notes [1],  the Java EE JAXB project released 2.4.0
> with changes to fix the issues they had deploying the JAXB runtime on
> the module path. It was a bit awkward before those changes in that you
> had to leave the runtime on the class path when running with
> java.xml.bind on the module path. I have no insight into why the
> download on the Eclipse site is 2.3.2 rather than the latest.
>
> -Alan
>
> [1] https://javaee.github.io/jaxb-v2/doc/user-guide/ch02.html#a-2-4-0
Reply | Threaded
Open this post in threaded view
|

Re: Problem with JAXBContext.newInstance

Alan Bateman
On 13/02/2019 12:05, Oliver Z. wrote:
> I found the solution. Pretty trivial, but probably unexpected when
> migrating. It is required to have 2 require entries in the
> module-info.java. I guess one for the API, and one for the
> implementation:
>
> requires java.xml.bind;
> requires com.sun.xml.bind;
This doesn't look right (at least not unless you are using a
implementation specific API exported by com.sun.xml.bind). This suggests
to me that java,xml.bind is missing `uses java.xml.bind.JAXBContext` or
com.sun.xml.bind is missing the equivalent `provides
java.xml.bind.JAXBContext with ...`. That or whatever your environment
isn't setting up the module path correctly.

-Alan