在 Java 类路径中的目录中包含所有 jar [英] Including all the jars in a directory within the Java classpath

查看:23
本文介绍了在 Java 类路径中的目录中包含所有 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我正在尝试 java -classpath lib/*.jar:.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 或更高版本,类路径选项支持通配符.请注意以下事项:

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

  • 使用直引号 (")
  • 使用*,而不是*.jar

Windows

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

Unix

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 "$(printf %s: lib/*.jar)"

(请注意,使用类路径与 -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 文档:

类路径条目可以包含基本名称通配符*,这被认为等同于指定所有文件的列表在扩展名为 .jar.JAR 的目录中.例如,类路径条目 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/barfoo/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.

通配符的扩展在调用a之前提前完成程序的主要方法,而不是在类加载期间延迟过程本身.输入类路径的每个元素都包含一个通配符被(可能是空的)元素序列替换通过枚举指定目录中的 JAR 文件生成.为了例如,如果目录 foo 包含 a.jarb.jarc.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 环境变量的处理方式与-classpath(或 -cp)命令行选项.也就是说,通配符是在所有这些情况下都很荣幸​​.但是,类路径通配符不是在 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

这篇关于在 Java 类路径中的目录中包含所有 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆