wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Unable to access a file in my Jar file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unable to access a file in my Jar file." Watch "Unable to access a file in my Jar file." New topic
Author

Unable to access a file in my Jar file.

Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
Hi All,
I'm having a strange problem.
I have an application where my class files access a properties file in my "resources" directory.
My directory structure is:
===================================================
MyApp:
--classes
--com
--mypackage
--*.classes
--src
--resources
--myproperties.properties
--myschema.xsd
======================================================
This works fine when I run my program directly.
But when I jar these files "com" and "resources" together, and try to run the executable jar file, I'm getting the FileNotFoundException when my code tries to access the file.
The file is accessed as-
============================================================
File myFile = new File("resources//myproperties.properties")
=============================================================
I've tried accessing the file using-
================================================================================== getClass.getClassLoader.getResourceAsStream("/resources//myproperties.properties")
===================================================================================
But this is giving me "null".

Can somebody help me out, how can I access the file which is archived the executable jar file itself?

Thanks,
Naresh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Are you really using // in part of the file name? Surely that ought to be /

CR
Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
I've tried using a single "/" too... but still it wasn't working.
But when I moved the resources folder to the directory structure of the class files' package, it started working fine.
Is it required that the resource files be at the package level of the main-class? I didn't know this.
damien malone
Ranch Hand

Joined: May 06, 2003
Posts: 35
Trying to access it as a file will never work from a jar, only the getResourceAsStream will work.

Its got to do with the class loader that is used. The File must be on the path of the classloader, so it should work anywhere from the root up along your com package structure.
Vasudhaiv Naresh
Ranch Hand

Joined: May 13, 2005
Posts: 57
The File must be on the path of the classloader, so it should work anywhere from the root up along your com package structure.


I've tried putting the "resources" directory at the level parallel to "com". Doing a getResourceAsStream("/resources/myproperties.properties") was giving me "null". But when I tried putting this at my package level, i.e, where the main class is, and doing a getResourceAsStream("/com/package/mypackage/resources/myproperties.properties") gave me the InputStream.
I'm just wondering y this is so? Is it mandatory that the "resources" be at the main class level?

Thanks,
Naresh
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Unable to access a file in my Jar file.