Java – use the classpath to override a file in a jar that is being run

classpathexecutable-jarjarjava

I have a JAR file that contains an application as well as configuration files for that application. The application loads configuration files from the classpath (using ClassLoader.getResource()), and has its dependencies completely satisfied using the configuration files baked into the JAR file.

On occasion I want the application to be run with a slightly different configuration (specifically I want to override the JDBC URL to point to a different database) so I create a new configuration file, store it in the correct directory structure (which means in a directory /config of a classpath entry), and I want to do something like this:

java -cp new-config:. -jar application.jar

But I can't get the classpath to have the new-config path entry before the application JAR's contents. Is it hard-coded that the JAR's content is always the first thing on the classpath?

Best Answer

Why not just invoke the application without specifying -jar and instead name the application main class explicitly? This will allow you to put both your new-config and the application.jar on the classpath in the required order:

e.g. (assuming "new-config" is a directory containing the overridden properties file)

java -cp new-config:application.jar Application.Main.Class

I believe the name of main class can be found in the MANIFEST.MF file inside the jar ....