This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

unable to load property file at runtime

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , i am a java beginner. My Jar file , has the following structure:

MyJar
|
sysProp.propertise
|
MyFolder
|
MySubFolder
|
main.class

------------------------------------

In my main class, i am trying to load property file like this :

main.class.getResource("sysProp.propertise");

This is not working. Please tell me what code is needed to load the property file.

Please help.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'It is not working' ist not a valid problem description. Are there by chance any exceptions thrown?


This code prints something like jar:file:/E:/Workspace/SomeProject/lib/somelib.jar!/some.file on my system. You can see the path within the jar file (it's directly under root, no subfolder inside the jar). If you'd like to read from a subfolder, you'd have to call something like
 
Owais Zahid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
URL result is NULL.

The property file is there in the jar file. I have checked it by un-jar the file. Is there any issue of loading the file from inner folders? My main file is in the Folder

MyFolder\MySubFolder\main.java

 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you're sure the JAR file is in your classpath? And you're sure you spelled your folder hierarchy and filename correctly?
 
Ranch Hand
Posts: 77
Android MyEclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check the path of the file

Please see the below example



main class have to search the file by comming back to two subfolders

so path would be ..//..//sysProp.propertise

 
Sheriff
Posts: 22584
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or just "/sysProp.properties". Using ClassLoader.getResource(AsStream) the path is always relative to the root of the JAR file. Using Class.getResource(AsStream) the path is always relative to the class file itself, unless if it starts with a /.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic