• Post Reply Bookmark Topic Watch Topic
  • New Topic

File not found as class resource in jar...

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys, I have a method that gets files that are local to a certain path.


But for some reason, once I package my application up in a jar file, it doesnt work.

any idea why ?

Please help.

(PS yes I have checked the inside of the jar file, and the text file is stored in the same directory as c.class and c.java.)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File represents real operating system Files, not JAR file entries. File.exists() won't work on a path inside a jar.

Just look to see if "c.getResource(localPath)" returns null. If it does, then the resource doesn't exist; if it returns a URL, then the resource exists. There's no need to ever convert the URL to a File.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then how can i package an application with resource files that are stored in classes ? ? This has to be possible somehow... ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what you mean by "stored in classes", but what I've described works whether the files are in a JAR or loose on disk.
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need the contents of the resource you should call getResourceAsStream instead. It returns an InputStream to the resource, or null if the resource does not exist.

Writing to a resource is not possible because it can be contained in a JAR file, which is essentially a ZIP file. You can't just write to ZIP files on the fly; programs that do usually copy the contents to a copy, then overwrite the original with that copy when done.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!