Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying classpath for a jar

I am trying to configure the classpath of a JAR so that my ResourceBundle can pick up property files from it.

If I run it from the .class files and specify the -cp flag it works fine, and System.err.println(System.getProperty("java.class.path")); will print the path specified in the -cp flag.

If I try and create a jar file for it, System.err.println(System.getProperty("java.class.path")); always prints the path of the jar file, and the property files aren't picked up.

It seems if you are running it as a jar file you can't specify the -cp flag (which was what I was hoping, as it's common to switch which property files are being used). I've tried specifying it in the jar manifest instead, but it's still not working.

Here is the code and manifest from a test jar that doesn't seem to work:

public final class Test {
    public static void main(final String[] args) {
        System.err.println(System.getProperty("java.class.path"));
    }
}

 

Manifest-Version: 1.0
Created-By: 1.6.0_20 (Sun Microsystems Inc.)
Main-Class: Test
Class-Path: /home/ajanuary/Projects/test/

edit The original path was rather meaningless so I changed it. I want to point to a directory which the ResourceBundle can find the property files in.

like image 854
ICR Avatar asked Jan 19 '23 16:01

ICR


2 Answers

If you use -jar, -cp is ignored:

-jar
Execute a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. In order for this option to work, the manifest of the JAR file must contain a line of the form Main-Class: classname. Here, classname identifies the class having the public static void main(String[] args) method that serves as your application's starting point. See the Jar tool reference page and the Jar trail of the Java Tutorial for information about working with Jar files and Jar-file manifests. When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

Source: java - the Java application launcher

like image 172
Sean Patrick Floyd Avatar answered Jan 30 '23 05:01

Sean Patrick Floyd


I would instead read a property in my Java application (that property could indicate from where resources should be loaded).

Example of how to execute the application would then be: java -Dkey=value -jar application.jar

like image 34
Kaj Avatar answered Jan 30 '23 04:01

Kaj