Java -jar : access external configuration file

Joel picture Joel · Feb 9, 2010 · Viewed 24.4k times · Source

I'm looking to do something which I thought was not going to be difficult.

I have an application that I'd like to package up as a jar because I've got ~30 dependencies and I would like to be able to deploy a single file.

I have some configuration files - a properties file and a spring configuration file, and my log4 props file - that I would like to have external to the jar. I guess I expected that if I put them in the same directory as the jar it would find them when it ran, but it doesn't.

While developing, I have these files at the root of the classpath for my eclipse project and the app finds them just fine. I feel like I'm missing some key aspect of jar / classpath theory...

so what I want is to be able to put the config files and the jar in the same directory and have the app find the config files when I run it with the standard java -jar thing.

Is there not a simple way to achieve this?

Answer

Joel picture Joel · Feb 18, 2010

I will preserve for posterity my solution to this problem.

I think it's possible that I was expecting java -jar to do something it doesn't do.

If you use the regular java command you can include the jar on the classpath and you'll end up with pretty much the same thing as java -jar, only you have to specifically name the class you want to use. It looks like this:

java -cp .:path/to/Jar.jar com.company.package.Class arg1=val1 arg2=val2 ....

Chances are you're going to build a script to start the program for you anyway, so the added complexity of the command line call doesn't really amount to much, since you'll only build it once anyway.

You'll see I included '.' as the first item on the classpath, which means anything in the same directory as the jar will be included on the classpath as well. Of course everything inside the jar is included on the classpath also, since the jar itself is on the classpath too.

You'll also see that I've shown how you can still hand in args on the command line. This was something I was unsure about when I started but it works as expected.

I'm sure this is just basic java deployment knowledge, but I was lacking it for some reason.

I hope this helps someone else. It certainly would have saved me a lot of time if someone had been able to say "don't bother trying to get the -jar thing working - just use the -cp method"...