2

I'm trying to use an XML resource file which I package inside a jar file (let's call it wrapper.jar)

I found this other thread which was very useful but I stumbled upon another problem. Within resources in JAR

My jar file content is as follows:

wrapper.jar:
/META-INF
/org
    /ihe
    axis2.xml

When I try this inside a class org.ihe.consumer.Foo constructor

java.net.URL xmlURL = getClass().getClassLoader().getResource("axis2.xml");

I get this from the "watch":

jar:file:/C:/Users/JROSE-HP/.m2/repository/org/apache/axis2/axis2-kernel/1.5.4/axis2-kernel-1.5.4.jar!/axis2.xml

I can't understand why it tries to get the xml file from the default axis2 jar instead of my jar... there is a dependency with axis-kernel-1.5.4.jar.

But how can I get the XMl file from wrapper.jar?

Thank you

Community
  • 1
  • 1
code-gijoe
  • 6,949
  • 14
  • 67
  • 103
  • You need to give the full path to the resources. If there are multiple files with the same path, the order of the classpath is important. The first entry found is returned. – Peter Lawrey Feb 15 '11 at 20:34
  • Ok I get it, I had the same resource name elsewhere. – code-gijoe Feb 15 '11 at 20:41

1 Answers1

2

you need to specify your path inside jar:

java.net.URL xmlURL = getClass().getClassLoader().getResource("org/ihe/axis2.xml");
lweller
  • 11,077
  • 3
  • 34
  • 38
  • I removed the axis2.xml ressource in my dependent project which had /org/axis2.xml also! Now it is loading the right one. – code-gijoe Feb 15 '11 at 20:42
  • Just to know, the "default" current URI, is it always the root package? – code-gijoe Feb 15 '11 at 20:44
  • I don't understand exactly what you mean with "default", but ressources are acces in same manner from classpath as classes are loaded from there. Pathes for resoures are juste separated with slashes in stead of dots. So If you have two resources with same path in different jars, the first one that wil be finded by classloader will be returned. – lweller Feb 16 '11 at 06:48