Blank Chisui
Blank Chisui

Reputation: 1073

jar and classpath wildcards

I want to have multiple classpath directories specified for a standalone java application under Win 7 with java 7 both 64bit.

The directory structure should be as follows

-app
|-lib
|-dynamicLib
|app.jar

the lib folder contains dependecies that are referenced directly in the manifest. everything in dynamicLib should be loaded as well.

I tried the following:

adding dynamicLib\* to the manifests classpath

adding dynamicLib to the commandline like this

java -cp "dynamicLib\*";app.jar my.mainclass 

the latter with and without quotes, absolute and relative paths, invertes slash etc. But both did not work.

So, how do you add a wildcard directory to an existing classpath when executing a jar?

Upvotes: 6

Views: 6741

Answers (4)

Ronald Bieber
Ronald Bieber

Reputation: 41

When starting an application from a .jar file, the command line argument -cp is ignored. Instead, the classpath needs to be specified in the MANIFEST.MF file's "Class-Path" attribute. Unfortunately, wildcards are not supported in this attribute, so all .jar files need to be named explicitly.

Therefore, the original idea of having a "dynamicLib" directory will only work if the names of the .jar files don't change.

Alternatively you can do without the central app.jar and work with .class files instead, thus allowing you to use wildcards at the command line level.

Upvotes: 2

martinez314
martinez314

Reputation: 12332

Use forward slash and put entire classpath in quotes:

java -cp "app.jar;dynamicLib/*" my.mainclass

Upvotes: 2

pabrantes
pabrantes

Reputation: 2181

From Oracle's documentation

... Class paths to the .jar, .zip or .class files. Each classpath should end with a filename or directory depending on what you are setting the class path to:

For a .jar or .zip file that contains .class files, the class path ends with the name of the .zip or .jar file.

For .class files in an unnamed package, the class path ends with the directory that contains the .class files.

For .class files in a named package, the class path ends with the directory that contains the "root" package (the first package in the full package name).

Source

This then means that -cp dynamiclib\* won't work. If dynamic lib contains jars, you must specify each jar in the classpath, if it contains class files then defining the folder (without the *) should be enough, unless you have classes defined in the unnamed package AKA the default package.

Upvotes: 0

yair
yair

Reputation: 9245

Classpath looks fine.

However, in order to execute a jar file, you have to use the -jar option.

java -cp "dynamicLib\*" -jar app.jar

See documentation.

-- UPDATE:

  1. using the above command loads only jar or JAR files from dynamicLib.
  2. wildcards aren't recursive: jar files under dynamicLib\nestedDir won't be loaded.

Upvotes: 1

Related Questions