将所有jar包含在Java类路径中的目录中
有没有办法在类路径的目录中包含所有jar文件?
Is there a way to include all the jar files within a directory in the classpath?
我正在尝试 java -classpath lib / *。罐:。 my.package.Program
,它无法找到肯定在这些jar中的类文件。我是否需要将每个jar文件分别添加到类路径中?
I'm trying java -classpath lib/*.jar:. my.package.Program
and it is not able to find class files that are certainly in those jars. Do I need to add each jar file to the classpath separately?
使用Java 6或更高版本,classpath选项支持通配符。请注意以下事项:
Using Java 6 or later, the classpath option supports wildcards. Note the following:
- 使用直引号(
)
- 使用
*
,而不是* .jar
- Use straight quotes (
"
) - Use
*
, not*.jar
Windows
java - cpTest.jar; lib / *my.package.MainClass
Unix
java -cpTest.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)
了解通配符
来自 Classpath 文档:
类路径条目可以包含basename通配符
*
,被认为等同于指定扩展名为.jar 的列表/ code>或
.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 entryfoo/*
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
中的JAR文件之前加载 foo
中的
类和资源,或者
反之亦然。
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文件,并且可能在平台到
平台之间变化,甚至在同一台机器上不时。
构造良好的应用程序不应该依赖于任何特定的
订单。如果需要特定的顺序,那么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.
在调用之前提前完成通配符的扩展一个
程序的主要方法,而不是迟到,在类加载
进程本身。包含
通配符的输入类路径的每个元素由枚举命名目录中的JAR文件生成的元素
替换(可能为空)元素。对于
示例,如果目录 foo
包含 a.jar
, b。 jar
, c.jar
,然后
类路径 foo / *
扩展为 foo / a.jar; foo / b.jar; foo / c.jar
,
,该字符串将是系统属性的值 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
b code> -classpath (或 -cp
)命令对$ c>环境变量的处理方式不同-line选项。也就是说,在所有这些情况下,通配符都是
。但是,在 Class-Path 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.
注意:由于java 8中的已知错误,windows示例必须使用带有尾随星号的条目前面的反斜杠: https://bugs.openjdk.java.net/browse/JDK-8131329
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