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 entry foo/* 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 either foo;foo/* or foo/*;foo. order chosen determines whether classes , resources in foo loaded before jar files in foo, or vice versa.

subdirectories not searched recursively. example, foo/* looks jar files in foo, not in foo/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 contains a.jar, b.jar, , c.jar, class path foo/* expanded foo/a.jar;foo/b.jar;foo/c.jar, , string value of system property java.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 in class-path jar-manifest header.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -