Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

java - 在Java类路径的目录中包含所有jar(Including all the jars in a directory within the Java classpath)

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

(有没有办法将所有jar文件包含在类路径的目录中?)

I'm trying java -classpath lib/*.jar:. my.package.Program

(我正在尝试java -classpath lib/*.jar:. my.package.Program)

java -classpath lib/*.jar:. my.package.Program and it is not able to find class files that are certainly in those jars.

(java -classpath lib/*.jar:. my.package.Program ,它无法找到这些jar中肯定存在的类文件。)

Do I need to add each jar file to the classpath separately?

(我是否需要将每个jar文件分别添加到类路径?)

  ask by Chris Serra translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using Java 6 or later, the classpath option supports wildcards.

(使用Java 6或更高版本时,classpath选项支持通配符。)

Note the following:

(请注意以下几点:)

  • Use straight quotes ( " )

    (使用双引号( " ))

  • Use * , not *.jar

    (使用* ,而不是*.jar)

Windows

(视窗)

java -cp "Test.jar;lib/*" my.package.MainClass

Unix

(Unix系统)

java -cp "Test.jar:lib/*" my.package.MainClass

This is similar to Windows, but uses : instead of ;

(这类似于Windows,但使用:代替;)

.

(。)

If you cannot use wildcards, bash allows the following syntax (where lib is the directory containing all the Java archive files):

(如果不能使用通配符,则bash允许使用以下语法(其中lib是包含所有Java归档文件的目录):)

java -cp $(echo lib/*.jar | tr ' ' ':')

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

((请注意,使用类路径与-jar选项不兼容。另请参见: 在命令提示符下执行具有多个类路径库的jar文件 ))

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 .

(类路径条目可以包含基本名称通配符* ,这被认为等效于指定目录中所有扩展名为.jar.JAR的文件的列表。)

For example, the class path entry foo/* specifies all JAR files in the directory named foo.

(例如,类路径条目foo/*指定目录foo中的所有JAR文件。)

A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

(仅由*组成的类路径条目将扩展为当前目录中所有jar文件的列表。)

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 .

(要在单个目录foo中匹配类和JAR文件,请使用foo;foo/*foo/*;foo 。)

The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo , or vice versa.

(选择的顺序确定是否在类和资源foo中之前在JAR文件加载foo ,或反之亦然。)

Subdirectories are not searched recursively.

(子目录不是递归搜索的。)

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

(例如, foo/*仅在foo查找JAR文件,而不在foo/barfoo/baz等中查找。)

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.

(在扩展的类路径中枚举目录中JAR文件的顺序未指定,并且可能因平台而异,甚至在同一台计算机上有时也有所不同。)

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.

(如果需要特定顺序,则可以在类路径中显式枚举JAR文件。)

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.

(输入类路径中包含通配符的每个元素都将由(可能为空)元素序列替换,这些元素序列是通过枚举命名目录中的JAR文件生成的。)

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 .

(例如,如果目录foo包含a.jarb.jarc.jar ,则类路径foo/*将扩展为foo/a.jar;foo/b.jar;foo/c.jar ,以及该字符串将是系统属性java.class.path的值。)

The CLASSPATH environment variable is not treated any differently from the -classpath (or -cp ) command-line option.

(CLASSPATH环境变量与-classpath (或-cp )命令行选项没有任何区别。)

That is, wildcards are honored in all these cases.

(也就是说,在所有这些情况下都应使用通配符。)

However, class path wildcards are not honored in the Class-Path jar-manifest header.

(但是,类路径通配符在Class-Path jar-manifest标头中不被接受。)

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

(注意:由于Java 8中的一个已知错误,Windows示例必须在前面的条目中使用反斜杠,并在其后加上星号: https : //bugs.openjdk.java.net/browse/JDK-8131329)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...