This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stax API conflicts with J2SE lib

 
Vu Pham
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm using Stax API (http://stax.codehaus.org/) in my project. I'm using Eclipse IDE also. But I'm having a small issue. I have a java file like below:


import javax.xml.XMLConstants;

...
final private static SchemaFactory SCHEMA_FACTORY =
SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
...


I want to use XMLConstants of J2SE lib. The problem is IDE just understands XMLConstants class here is of Stax API. XMLConstants class of Stax API doesn't have W3C_XML_SCHEMA_NS_URI property. That's why Eclipse can't compile my java file. I can't remove Stax API from my program because there are some others places I used it. Do you know how to solve this one? Thanks very much.

Kind regards,
Vu Pham
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The import should make sure you're using the JSE class and not the Stax class. It's only available since Java 5.0 though. Are you perhaps still using Java 1.4?
Single import statements always take precedence over wildcard import statements (e.g. import java.util.*) so with that line either you are using the right class or the entire import will cause a compiler error.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I thought at first too, Rob, but acting on a hunch, I downloaded the jar (thanks for the link!) The API jar file actually includes its own copy of javax/xml/XMLConstants.class , as vile as that may seem to us.

My recommendation to you, Vu Pham, would be to build your own stax.jar which didn't include this class; just removing the file from the jar. You could just un-jar it, delete that one class, and re-jar it.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But shouldn't classes in rt.jar take precedence over any other classes because they are loaded by the bootstrap class loader?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'd think. Maybe he's got stax installed as a standard extension (in /ext?)
 
Vu Pham
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the order of JAR file in "Configure Build Path". It's ok now :-)

Thanks all for your help :-)
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figures. I think Eclipse uses its own bootstrap class loader and ignores the JVM that has loaded it. Instead it only looks at the project properties.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic