• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding a property file to a jar

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

i want to deliver a jar to a client in which some of the classes read some values from property files.My question is

1)Is it a good idea to put property files in a jar?
2)if so how can i put a property file inside a jar?
3)If its not how can i distribute it as it may go to different persons and they may have different structures?It will be difficult for me to assume that it will be placed in a particular directory.

Any inputs are highly apreciated.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

1. It can be a good idea to put your properties file in the JAR. A disadvantage of putting it inside the JAR is that it will be harder to edit (you'd have to extract it from the JAR and put it back in).

2. When you build the JAR file, just include it together with your class files. You can use the method getResourceAsStream() in class Class or ClassLoader to read your properties file inside the JAR file. Suppose, for example, that you have your properties file in the root of the JAR file, then you could read it inside your program like this:

[ September 03, 2008: Message edited by: Jesper Young ]
 
pavan babu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jesper ,

the solution you gave works when i want to read the property file from a class out side the jar.But my problem is there are some classes inside the jar which access my property files.when I add the properties file to jar and use it in another application it is throwing resource not found exception.

Is there a way to resolve it ? Does it make sense ?
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

use it in another application it is throwing resource not found exception.


How does the other application access the jar file with the properties file?
Is it on the JVM's classpath? What method does the app use?
 
pavan babu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the jar is placed in the classpath and there are three methods which access the property file inside the jar.I am using fileinputstream class to read the properties file.
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pavan babu:
I am using fileinputstream class to read the properties file.



Please read what Jesper wrote...
 
Ranch Hand
Posts: 31
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jesper! I realize this is an almost 4 year old post- but it helped me resolve my issue. Id been trying to read a properties file from a jar using the following:

FileInputStream fin = new FileInputStream(path);
props = new Properties();
props.load(fin);

Id also been using FileInputStream(like Pavan) and it wasnt working for some reason (Ive still got to figure out why). Once I replaced the code with the following, it worked!

InputStream fin=null;
fin=ClassLoader.getSystemResourceAsStream(path); //this is a static function
props = new Properties();
props.load(fin);

A small note regarding the "path" variable mentioned above. "path" is the path to my properties file relative to where my main class is. So, if my main is com.test and the properties file is x.properties, I placed x.properties outside com when creating the jar and path was simply x.properties.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic