且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在java类路径中设置多个jars

更新时间:2022-01-25 01:37:37

使用Java 6或更高版本,classpath选项支持通配符。请注意以下事项:

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


  • 使用直引号(

  • 使用 * ,而不是 *。jar

  • Use straight quotes (")
  • Use *, not *.jar

Windows


cpTest.jar; lib / *my.package.MainClass

/ strong>

Unix

java -cpTest.jar:lib / *my.package.MainClass

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

这与Windows类似,但使用而不是; 。如果不能使用通配符, bash 允许以下语法(其中 lib 是包含所有Java归档文件的目录):

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 $ (echo lib / *。jar | tr''':')

classpath与 -jar 选项不兼容。另见:从命令提示符执行多个类路径库的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

Classpath 文档:


类路径条目可以包含基本名称通配符 * ,这被认为等同于指定扩展名为 .jar 或的列表c $ c> .JAR 。例如,
类路径条目 foo / * 指定名为
foo的目录中的所有JAR文件。类路径条目简单地由 * 扩展为当前目录中的所有
jar文件的列表。

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.

包含 * 的类路径条目将不匹配类文件。要
匹配单个目录foo中的类和JAR文件,请使用
foo; foo / * foo / *; foo 。选择的顺序决定 foo 中的
类和资源是否在 foo 中的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. The order chosen determines whether the classes and resources in foo are loaded before JAR files in foo, or vice versa.

不递归搜索子目录。例如, foo / * 仅在 foo 中查找
的JAR文件,而不是 foo / bar foo / baz 等。

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

其中在
扩展类路径中枚举目录中的JAR文件未被指定,并且可以从平台到
平台,甚至在同一机器上的时刻不同。 A
构建良好的应用程序不应取决于任何特定的
命令。如果需要特定的顺序,那么JAR文件可以是类路径中明确枚举的

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.

扩展通配符在调用a
程序的主要方法,而不是晚期,在类加载
过程本身。包含
通配符的输入类路径的每个元素都由枚举所命名目录中的JAR文件生成的元素
的(可能为空)序列替换。对于
示例,如果目录 foo 包含 a.jar b。 jar c.jar ,然后
类路径 foo / * 扩展为 foo / a.jar; foo / b.jar; foo / c.jar
,并且该字符串将是system属性的值
java.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.

CLASSPATH 环境变量与
的处理方式不同, -classpath (或 -cp ) -line选项。也就是说,通配符在所有这些情况下都是
。但是,类路径通配符在类路径jar-manifest 头中不是

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.