Java – jar: accessing external configuration files

I'm looking for something that I don't think will be difficult

I have an application. I want to package it into a jar. Because I have ~ 30 dependencies, I want to be able to deploy a file

I have some configuration files - a properties file and a spring configuration file, and my log4 props file - I want to be outside the jar I think I expected that if I put them in the same directory as jar, it would find them at run time, but it didn't

During development, I put these files in the root directory of the classpath of the eclipse project, and the application found them very good I think I missed some key aspects of jar / classpath theory

So what I want is to put the configuration file and jar in the same directory, and when I run it using standard Java jar things, the application will find the configuration file

Is there a simple way to achieve this?

Solution

I will reserve my solution to this problem for future generations

I think I might expect Java jar to do something it doesn't

If you use regular Java commands, you can include the jar in the classpath, and it will eventually be almost the same as Java jar, except that you must specifically name the class to be used It looks like this:

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

It's possible that you need to build a script to start the program for you, so the added complexity of command line calls is not too much, because you only build it once

You'll see me including ' As the first item in the classpath, this means that anything in the same directory that is the same as jar will also be included in the classpath Of course, everything in jar is included in the classpath, because jar itself is also on the classpath

You will also see that I have shown how args can still be submitted on the command line I'm not sure when I start, but it works as expected

I'm sure this is only basic Java deployment knowledge, but for some reasons I lack this knowledge

I hope it helps others If someone can say "don't try to get - Jar - just use - CP method", it can save a lot of time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>