Including all the jars in a directory within the Java classpath

前端 未结 24 3610
鱼传尺愫
鱼传尺愫 2020-11-21 04:25

Is there a way to include all the jar files within a directory in the classpath?

I\'m trying java -classpath lib/*.jar:. my.package.Program and it is no

相关标签:
24条回答
  • 2020-11-21 05:03

    Using Java 6 or later, the classpath option supports wildcards. Note the 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 is similar to Windows, but uses : instead of ;. If you cannot use wildcards, bash allows the following syntax (where lib is the directory containing all the Java archive files):

    java -cp "$(printf %s: lib/*.jar)"

    (Note that using a classpath is incompatible with the -jar option. See also: Execute jar file with multiple classpath libraries from command prompt)

    Understanding Wildcards

    From the Classpath document:

    Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/* specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

    A class path entry that contains * will not match class files. To match both classes and JAR files in a single directory foo, use either foo;foo/* or foo/*;foo. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

    Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.

    The order in which the JAR files in a directory are enumerated in the expanded class path is not specified and may vary from platform to platform and even from moment to moment on the same machine. A well-constructed application should not depend upon any particular order. If a specific order is required then the JAR files can be enumerated explicitly in the class path.

    Expansion of wildcards is done early, prior to the invocation of a program's main method, rather than late, during the class-loading process itself. Each element of the input class path containing a wildcard is replaced by the (possibly empty) sequence of elements generated by enumerating the JAR files in the named directory. For example, if the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property java.class.path.

    The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp) command-line option. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

    Note: due to a known bug in java 8, the windows examples must use a backslash preceding entries with a trailing asterisk: https://bugs.openjdk.java.net/browse/JDK-8131329

    0 讨论(0)
  • 2020-11-21 05:04

    Windows:

     java -cp file.jar;dir/* my.app.ClassName
    

    Linux:

     java -cp file.jar:dir/* my.app.ClassName
    

    Remind:
    - Windows path separator is ;
    - Linux path separator is :
    - In Windows if cp argument does not contains white space, the "quotes" is optional

    0 讨论(0)
  • 2020-11-21 05:04

    Please note that wildcard expansion is broken for Java 7 on Windows.

    Check out this StackOverflow issue for more information.

    The workaround is to put a semicolon right after the wildcard. java -cp "somewhere/*;"

    0 讨论(0)
  • 2020-11-21 05:07

    I have multiple jars in a folder. The below command worked for me in JDK1.8 to include all jars present in the folder. Please note that to include in quotes if you have a space in the classpath

    Windows

    Compiling: javac -classpath "C:\My Jars\sdk\lib\*" c:\programs\MyProgram.java

    Running: java -classpath "C:\My Jars\sdk\lib\*;c:\programs" MyProgram

    Linux

    Compiling: javac -classpath "/home/guestuser/My Jars/sdk/lib/*" MyProgram.java

    Running: java -classpath "/home/guestuser/My Jars/sdk/lib/*:/home/guestuser/programs" MyProgram

    0 讨论(0)
  • 2020-11-21 05:08

    If you really need to specify all the .jar files dynamically you could use shell scripts, or Apache Ant. There's a commons project called Commons Launcher which basically lets you specify your startup script as an ant build file (if you see what I mean).

    Then, you can specify something like:

    <path id="base.class.path">
        <pathelement path="${resources.dir}"/>
        <fileset dir="${extensions.dir}" includes="*.jar" />
        <fileset dir="${lib.dir}" includes="*.jar"/>
    </path>
    

    In your launch build file, which will launch your application with the correct classpath.

    0 讨论(0)
  • 2020-11-21 05:08

    The only way I know how is to do it individually, for example:

    setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.
    

    Hope that helps!

    0 讨论(0)
提交回复
热议问题