java jar 目录_将Java类路径中的所有jar包括在一个目录中
有沒有一種方法可以將所有的jar文件包含在類路徑的目錄中?
我正在嘗試java -classpath lib / *。jar :. my.package.Program,它無法找到當然在這些罐子里的類文件。我是否需要將每個jar文件分別添加到類路徑中?
使用Java 6或更高版本,classpath選項支持通配符。請注意以下幾點:使用直引號(“)”
使用*,而不是*。jar
窗java -cp "Test.jar;lib/*" my.package.MainClass
的Unix
java -cp "Test.jar:lib/*" my.package.MainClass
這與Windows類似,但使用:而不是;。如果不能使用通配符,bash允許使用以下語法(其中lib是包含所有Java歸檔文件的目錄):
java -cp $(echo lib/*.jar | tr ' ' ':')
(請注意,使用類路徑與-jar選項不兼容。另請參見:從命令提示符執行帶有多個classpath庫的jar文件)
了解通配符
從Classpath文件:<
/ p>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.
在windows下這個工作:java -cp "Test.jar;lib/*" my.package.MainClass
這不起作用:java -cp "Test.jar;lib/*.jar" my.package.MainClass
注意.jar, **所以應該單獨使用通配符** 。
在Linux上,以下工作:java -cp "Test.jar:lib/*" my.package.MainClass
分隔符是冒號而不是分號。
未經作者同意,本文嚴禁轉載,違者必究!
總結
以上是生活随笔為你收集整理的java jar 目录_将Java类路径中的所有jar包括在一个目录中的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么减肥熬夜不掉秤
- 下一篇: 减肥吃红薯会发胖吗