Specifying a class path for a can

I am trying to tune the path to JAR classes so that my ResourceBundle can extract property files from it.

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

If I try to create a jar file for it, System.err.println(System.getProperty("java.class.path")); always prints the path to the jar file, and property files are not matched.

It seems that if you use it as a jar file, you cannot specify the -cp flag (this was what I hoped for, as it is often used to switch which property files are used). I tried specifying it in the jar manifest instead, but it still doesn't work.

Here is the code and manifest from the test jar, which 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/ 

change The original path was pretty pointless, so I changed it. I want to point to the directory that the ResourceBundle can find in the properties file.

+4
source share
5 answers

The problem was that I also had an index file. If you have an index file, Class-Path will be ignored.

0
source

If you use -jar, -cp is ignored:

-jar
Run the program encapsulated in the JAR file. The first argument is the JAR file name instead of the launch name. For this to work, the manifest JAR file must contain the line form Main-Class: classname. Here, classname defines a class that has the method public static void main(String[] args) , which serves as your starting point for the application. See the Jar and Jar tool reference page for a Java tutorial for information on working with Jar files and manifest Jar files. When you use this parameter, the JAR file is the source of all user classes and other parameters of the user class path are ignored.

Source: java - Java Application Launcher

+5
source

I would instead read the property in my Java application (this property can indicate where the resources should be loaded from).

An example of how to run the application will be as follows: java -Dkey=value -jar application.jar

+2
source

You cannot use classpath wildcards in a manifest.

See Setting the classpath for more information on how the classpath works:

Class wildcards

are not executed in the jar-manifest header of the Path class.

+1
source

Yes, for the shell ~ means $ HOME, but for java it means nothing.

+1
source

All Articles