• Post Reply Bookmark Topic Watch Topic
  • New Topic

getResource() not working in a jar with Class-Path attribute  RSS feed

 
Pierre-Henri Cross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've a problem i've stumbled upon for many days and I can't understand what's happening.

I've developped an application in the Eclipse IDE that runs mighty fine in the IDE, but when i try to put it in a .jar and execute it, every resource i try to access with a getResource() returns null, as if the jar itself wasnt in the classpath.

Here's how i do it :
The jar in question is called "HL7Explorer.jar".
It is built with an ant task. The manifest generated looks like this :



The classes are organized in packages :

\_application
  \_ HL7Explorer.class
\_META-INF
  \_ MANIFEST.MF
\_utils
  \_ Constants.class
  \_ MGFUtils.class
  \_ HL7def.xml
\_...

The code calling getResource() is accessed this way :




If i do a System.out.println(Constants.class.getResource("./HL7def.xml").getFile()), it returns null when executed with the command:
java -jar HL7Explorer.jar
It returns the name of the file when executed in the IDE.

Have any of you come across this problem ?
I feel it's a classpath problem but i haven't been able to fix it yet.
Am i going in the right direction. Does anyone have a hint ?

Thanks for reading me, feel free to ask questions

Pierre-Henri
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can access a file in a JAR, but you cannot get a java.io.File reference to it, since that only works with regular files on the harddrive.
You can however get a URL reference to it, and you are also able to getResourceAsStream() to read the contents.
The lesson here is that you should try to avoid passing java.io.File handles around, and instead you should read the properties and return the parsed contents for use by the rest of the application.
 
Pierre-Henri Cross
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your quick feedback, i will try is asap

EDIT: works a charm. Thank you very much !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!