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?
Are you really using // in part of the file name? Surely that ought to be /
Joined: May 13, 2005
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.
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.
Joined: May 13, 2005
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?