13

I recently converted an eclipse Java project into a dynamic web project. The imported jars listed in both the before and after projects are the same, but the change to dynamic web project causes the following compilation error:

W3C_XML_SCHEMA_NS_URI cannot be resolved or is not a field  

to be thrown by the following line of code:

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

I have researched this error, and it seems to be thrown by conflicting versions of javax.xml.XMLConstants in different jars, but I compared the lists of jars in both projects and they are identical, so I think one has to change the order of the jars. How does one do this?

Part of the solution might logically involve figuring out which jars include a package named javax.xml.XMLConstants. So I followed @DiogoSantana's advice and used the Type wizard to get the results in the following print screen:

I then followed DiogoSantana's advice and ran mvn dependency:tree and got the following results:

I next made the following change to the pom.xml:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.2.7</version>
    <exclusions>
        <exclusion>
            <groupId>jsr173_api</groupId>
        </exclusion>
    </exclusions>
</dependency>

And then I ran mvn clean install before refreshing the eclipse project and even doing maven..update project from within eclipse, but the error remains.

Note: a search for the string infoset in the pom did not produce any results, so I tried the next higher level jar.

Community
  • 1
  • 1
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • When you converted the project to dynamic web project, eclipse add the jars from the server runtime to the project classpath. The conflict should be because of this. Use Open Type Wizard (Ctrl Shift T) to see the jars with that class. – DiogoSantana Oct 23 '14 at 23:50
  • 1
    I got ask, are you using maven? – DiogoSantana Oct 24 '14 at 00:09
  • You are using a old jar version of XMLConstants. Try to exclude the jsr173_api from your project. – DiogoSantana Oct 24 '14 at 00:12
  • @DiogoSantana How do I do that? – CodeMed Oct 24 '14 at 00:13
  • These days (in Java 6 and newer), you do not need any explicit JAXB, JAX-WS or XML Streaming API (JSR-173) jars because they are all included in the JDK. You should exclude anything to do with these if your environment is Java 6 or newer. That said, the HyperJaxb3 project looks awfully dead right now... – Steve C Oct 24 '14 at 01:03
  • @SteveC commenting out the entire `jaxb-impl` dependency removed the compilation error warning, but I cannot test it until I fix some `jpa` warning settings in `eclipse` that will take me a little while to research tonight. You say `hyperjaxb` is dead. How else would you recommend that I autogenerate `hibernate` annotations from an `xsd` file? – CodeMed Oct 24 '14 at 01:19
  • Looking dead and being dead are two different things. Perhaps some of the other solutions at [XSD Schema - JAXB marshaling - Datastore(JPA/JDO) Roundtrip](http://stackoverflow.com/questions/4086388/xsd-schema-jaxb-marshaling-datastorejpa-jdo-roundtrip) could be useful. Of course the beauty of open source means that there is nothing stopping you from "modernising" the existing hyperjaxb code base and contributing it back. It might be fun. – Steve C Oct 24 '14 at 02:02
  • @SteveC I just ran a test and commenting out the entire `jaxb-impl` dependency in my posting above did indeed resolve the error. Probably for the reason you gave, that Jaxb must already be included in the jdk. If you want to put what you said in an answer, I will mark it as accepted and +1. – CodeMed Oct 24 '14 at 02:21

5 Answers5

57

I had the same problem and was able to solve it by doing the following :

  1. Right click the module which contains the file with error.
  2. Click "Properties"
  3. From left, select "Java Build Path"
  4. Now on right, select the "Order and Export" tab
  5. Click on the "JRE System Library" and click on the "Up" button to move it to the top of "Maven Dependencies"
user4572219
  • 571
  • 4
  • 3
7

To fix this, open up the Build-Path menu of your project, switch to "Order and Export" tab, and ensure that the Java System library is in the top of the list than the Maven dependencies.

Ahmed MANSOUR
  • 2,369
  • 2
  • 27
  • 35
4

These days (in Java 6 and newer), you do not need any explicit JAXB, JAX-WS, XML Streaming API (JSR-173) or SAAJ jars because they are all included in the JDK. There are many other non-xml related APIs generally available too, such as javax.activation for example.

You should exclude anything to do with these if your environment is Java 6 or newer.

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • 1
    I used this again today when I changed my project compliance to JDK7. I read your suggestions here, then used the `Type wizard` in `eclipse`, saw that `JDK7` includes something that is also in a separate `jar`, then googled the `jar` name to find the `maven artifact`, then commented out that `maven artifact` in my `pom.xml` . Thank you. – CodeMed Jan 19 '15 at 22:33
1

Fisrt use mvn dependency:tree to determine the dependency that depends of jsr173_api.

Supposed it is hibernate, like you mentioned (I don't know if that is true, didn't check it out). You need to exclude the jsr173_api like this:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.3.6.Final</version>
    <exclusions>
        <exclusion>
            <groupId>jsr173_api</groupId>
        </exclusion>
    </exclusions>
</dependency>
DiogoSantana
  • 2,404
  • 2
  • 19
  • 24
  • Run dependency:tree again and check if jsr173 is gone. Didn't you run mvn eclipse:eclipse to maven reconfigure eclipse project? – DiogoSantana Oct 24 '14 at 01:11
  • +1 and thank you again for all your help. I resolved the problem by following @SteveC 's advice to comment out references to jaxb from the pom. – CodeMed Oct 24 '14 at 02:22
0

Just to skip the error & proceed to execute the code just include the below line in your code. It will work.

final String W3C_XML_SCHEMA_NS_URI = "http://www.w3.org/2001/XMLSchema";
Harsha
  • 39
  • 1
  • 7