Mike Thomsen
Mike Thomsen

Reputation: 37506

Load a config file into the classpath while using an executable jar

I am building an assembly in Maven for a command line utility. I can run it as an executable jar, but it fails because I need to load the config file externally. Assuming the following config, how would I run the jar?

  1. Jar is in /opt/myapp/lib/myapp-assembly.jar
  2. Config is in /etc/myapp/config/settings.xml
  3. I'm loading the code from the classpath using ClassPathResource("/settings.xml");

Any help is appreciated!

Upvotes: 0

Views: 1927

Answers (1)

user597474
user597474

Reputation:

I see two ways you could do it:

  1. Launch the program using the jar as an archive rather than an executable jar, specifying the main class at run time. In other words, do java -classpath /opt/myapp/lib/myapp-assembly.jar:/etc/myapp/config [name of your main class].
  2. Use the Class-Path field of the jar manifest file. Entries in it are directly added to the run time classpath, and there's nothing stopping you from specifying a filesystem directory rather than another jar file. So your manifest would contain: Class-Path: /etc/myapp/config/

Upvotes: 1

Related Questions