command line - Including all the jars in a directory within the Java classpath -
is there way include jar files within directory in classpath?
i'm trying java -classpath lib/*.jar:. my.package.program
, not able find class files in jars. need add each jar file classpath separately?
using java 6 or later, classpath option supports wildcards. note following:
- use straight quotes (
"
) - use
*
, not*.jar
windows
java -cp "test.jar;lib/*" my.package.mainclass
unix
java -cp "test.jar:lib/*" my.package.mainclass
this similar windows, uses :
instead of ;
. if cannot use wildcards, bash
allows following syntax (where lib
directory containing java archive files):
java -cp $(echo lib/*.jar | tr ' ' ':')
(note using classpath incompatible -jar
option. see also: execute jar file multiple classpath libraries command prompt)
understanding wildcards
from classpath document:
class path entries can contain basename wildcard character
*
, considered equivalent specifying list of files in directory extension.jar
or.jar
. example, class path entryfoo/*
specifies jar files in directory named foo. classpath entry consisting of*
expands list of jar files in current directory.a class path entry contains
*
not match class files. match both classes , jar files in single directory foo, use eitherfoo;foo/*
orfoo/*;foo
. order chosen determines whether classes , resources infoo
loaded before jar files infoo
, or vice versa.subdirectories not searched recursively. example,
foo/*
looks jar files infoo
, not infoo/bar
,foo/baz
, etc.the order in jar files in directory enumerated in expanded class path not specified , may vary platform platform , moment moment on same machine. well-constructed application should not depend upon particular order. if specific order required jar files can enumerated explicitly in class path.
expansion of wildcards done early, prior invocation of program's main method, rather late, during class-loading process itself. each element of input class path containing wildcard replaced (possibly empty) sequence of elements generated enumerating jar files in named directory. example, if directory
foo
containsa.jar
,b.jar
, ,c.jar
, class pathfoo/*
expandedfoo/a.jar;foo/b.jar;foo/c.jar
, , string value of system propertyjava.class.path
.the
classpath
environment variable not treated differently-classpath
(or-cp
) command-line option. is, wildcards honored in these cases. however, class path wildcards not honored inclass-path jar-manifest
header.
Comments
Post a Comment