Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to load property file at runtime

 
Owais Zahid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
damodar kumar
Ranch Hand
Posts: 77
Android Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • 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

 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 /.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic