• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to access a file in my Jar file.

 
Vasudhaiv Naresh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really using // in part of the file name? Surely that ought to be /

CR
 
Vasudhaiv Naresh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic