Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAXB issues under Java 7 it says it can't find RI 2.1.0

I am running a test on Ubuntu with Oracle Java 7 Update 3. According to the release notes it has JAXB-2.2.4 included with it.

/home/ubuntu# update-alternatives --config java
There is only one alternative in link group java: /usr/lib/jvm/java-7-oracle/bin/java
Nothing to configure.

The machine says it has JAXB-2.2.4 on it:

$ wsimport -version
JAX-WS RI 2.2.4-b01

$ java -version

java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode)

However when I run my app which uses JAX-RS and marshalls some objects with JAXB I get the following error:

Apr 05, 2012 10:45:50 AM com.sun.jersey.api.json.JSONConfiguration natural
SEVERE: NATURAL JSON notation configured, but JAXB RI 2.1.10 not found. For the
recent builds to get this working correctly, you need even at least JAXB version
 2.1.12. Please add it to your classpath!
Apr 05, 2012 10:45:50 AM com.sun.jersey.core.spi.component.ProviderFactory __get
ComponentProvider
SEVERE: The provider class, class com.marketchorus.rest.config.JAXBContextResolv
er, could not be instantiated. Processing will continue but the class will not b
e utilized
java.lang.RuntimeException: NATURAL JSON notation configured, but JAXB RI 2.1.10
 not found. For the recent builds to get this working correctly, you need even a
t least JAXB version 2.1.12. Please add it to your classpath!

On the server side I got that same error initially so I installed metro-2.2-standalone into tomcat which fixed the issue. (Even though it was running in java 7).

On a windows client I was forced to add JAXB-2.2.5 api, impl, and jsr173 jars to my endorsed java directory to make it work (with Java 7 update 3 there as well).

When I try to do the jaxb trick under ubuntu I put the files under /usr/lib/jvm/java-7-oracle/jre/lib/endorsed I get a different error:

ERROR 10:55:44.694 taskScheduler-1 org.springframework.scheduling.support.TaskUt
ils$LoggingErrorHandler - Unexpected error occurred in scheduled task.
javax.xml.stream.FactoryConfigurationError: Provider com.bea.xml.stream.MXParser
Factory not found

I am initializing the JAX-RS/JAXB code in my class as follows:

config = new DefaultClientConfig();
config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
config.getClasses().add(JAXBContextResolver.class);
client = Client.create(config);
service = client.resource(UriBuilder.fromUri(proc.getUrl()).build());

I tried to compile it with source=1.7 and target=1.7 as it was still set for 1.6 and I was thinking maybe it was assuming the old version from the early 1.6 releases which was too old but that doesn't solve the problem either.

I ran @andih's test program and I still get the error. I am starting to think it is a VM issue:

ubuntu@ip-10-202-155-29:~/test$ java -cp jersey-client-1.12.jar:jersey-core-1.1
2.jar:jersery-fastinfoset-1.12.jar:jersey-json-1.12.jar:. TestNaturalJson
Apr 23, 2012 10:17:21 AM com.sun.jersey.api.json.JSONConfiguration natural
SEVERE: NATURAL JSON notation configured, but JAXB RI 2.1.10 not found. For the
recent builds to get this working correctly, you need even at least JAXB version
 2.1.12. Please add it to your classpath!
Exception in thread "main" java.lang.RuntimeException: NATURAL JSON notation con
figured, but JAXB RI 2.1.10 not found. For the recent builds to get this working
 correctly, you need even at least JAXB version 2.1.12. Please add it to your cl
asspath!
        at com.sun.jersey.api.json.JSONConfiguration.natural(JSONConfiguration.j
ava:447)
        at TestNaturalJson.main(TestNaturalJson.java:6)
ubuntu@ip-10-202-155-29:~/test$ java -version
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode)

Looking back at the endorsed solution with JAXB-2.2.5 RI I installed the files:

jaxb-api.jar
jaxb-impl.jar
jsr173_1.0_api.jar

into the:

/usr/lib/jvm/java-7-oracle/jre/lib/endorsed

Directory. When I do that I get the above error from Spring the MXParser error. The wierd thing here is under windows this works, but not on Ubuntu. When I run @andhi's test program below with the endorsed setup I get OK instead of the error. But for some reason it looks like Spring under Ubuntu isn't picking up the JSR173 jar file that is installed in the endorsed dir (which is the STAX implementation).

like image 665
haskovec Avatar asked Apr 05 '12 16:04

haskovec


2 Answers

Seems to me like a Classpath Problem. Have you checked if there other jaxb-impl-x.y.z.jar or jaxb-api-k.l.m.jars in your classpath? The JSonConfiguration looks for the class com.sun.xml.bind.annotation.OverrideAnnotationOf which is part of the JRE7 rt.jar.

You can check your JRE installation using with the following simple programm:

import com.sun.jersey.api.json.JSONConfiguration;


public class TestNaturalJSon {
    public static void main(String[] args) {
        JSONConfiguration config = JSONConfiguration.natural().build();

        System.out.println("OK");

    }

}

If you use maven you can use a pom with the following dependencies:

<dependencies>
  <dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.12</version>

    <exclusions>
        <exclusion>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-impl</artifactId>
        </exclusion>
        <exclusion>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        </exclusion>
    </exclusions>

   </dependency>
</dependencies>
like image 108
andih Avatar answered Oct 20 '22 20:10

andih


To know from where a Class is loaded, you should run the JVM command line with -verbose:class option.

That way, you can guess what is wrong by looking at latest verbose lines just before the javax.xml.stream.FactoryConfigurationError exception is thrown, or just before the diagnostic message JAXB RI 2.1.10 not found.

So you can get the jar an implementation comes from and check that the endorsed directory is used or not...

like image 21
Yves Martin Avatar answered Oct 20 '22 19:10

Yves Martin