• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Unable to access a file in my Jar file.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80267
430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
World domination requires a hollowed out volcano with good submarine access. Tiny ads are optional.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic