aspose file tools
The moose likes Java in General and the fly likes properties file in a jar Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "properties file in a jar" Watch "properties file in a jar" New topic
Author

properties file in a jar

Michael MacEachran
Greenhorn

Joined: Dec 15, 2009
Posts: 11
All,

I have a standard dev/test/prod enviroment. I also have my core classes in myapp.jar. I have your basic db connect string for each environment. So I am trying to do this right and have a properties file instead for hard coding a different string for each DB (among other things)

The problem is that the jar file is being used by many applications. Stand alone executable jar, web services, web applications, Eclipse IDE, etc... so my relative file path is different every time and - fails - the path is different every time

I cold hard code it from the root, but that's wrong, and it's different on every box.

There has to be a way of doing it so the path is relative to the jar file. Something like a class.forName("com.myapp.properties") so that nomater where the jar is being used, it can find the properties file.

Also, it should be easy to deploy with ant, like I copy the correct properties file into the /bin directory and simply jar it up.

Any ideas?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1409

The trick to loading a .properties file (or any resource really) packaged inside a .jar file is using the ClassLoader#getResource???() family of methods.
Have a look at the documentation and see what comes up with a quick Google search.
You should be able to figure it out that way.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Michael MacEachran
Greenhorn

Joined: Dec 15, 2009
Posts: 11
What am I doing wrong? I have tried every permutation of something like this:



I have tried all of these:
props.load(classLoader.getResourceAsStream("com.myapp.props.properties")); // with the file named "properties"
props.load(classLoader.getResourceAsStream("my.properties")); // with the file in the same directory as the class
props.load(classLoader.getResourceAsStream("my.properties")); // with the file in root directory of the jar

Any ideas?

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2


This path is relative to the package of the current class. So if your class is named com.myapp.banana.Init, then the getResource() type methods will be looking for the path "/com/myapp/banana/com/myapp/props/my.properties" in the jar.

You must have missed the bit in the documentation which says to use a slash at the beginning if you want an absolute path. Like this:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Paul Clapham wrote:
This path is relative to the package of the current class.

Only if you use Class.getResource(AsStream). With ClassLoader.getResource(AsStream) it's relative to the class path. In this case I'd use the following:

Michael MacEachran wrote:What am I doing wrong? I have tried every permutation of something like this:


That should work, but isn't your file called myapp.properties? It is according to your initial post.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Michael MacEachran
Greenhorn

Joined: Dec 15, 2009
Posts: 11
Well, I cant get either of them to work. Now, because I have several classes that need a properties file I thought that I would move the my.properties to the root of the jar file and do this:



I open my jar file and at the root I have 2 directories com and META-INF and one file my.properties

that should work right? I am getting a NullPointerException
Michael MacEachran
Greenhorn

Joined: Dec 15, 2009
Posts: 11
I have given up. My solution:

I set environment variables for what I want and used System.getenv()

Works on all platforms and class loaders!

Thank you for the help. I think the problem is that this jar is being used in many ways by many apps with different ways of loading the classes, so nothing seemed to work in all situations.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You shouldn't use a leading / when using ClassLoader to load the resources. That's only allowed when using Class to load them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: properties file in a jar
 
Similar Threads
(NX)path of db file
Accessing files that are there in classes directory
Properties File sharing across Mulipe Web applications
JBoss class loading and reading .properties files
NX:window XP - java.io.FileNotFoundException