在Java类路径中包含目录中的所有jar

时间:2022-09-01 22:52:20

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 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 -classpath lib/*.jar:。my.package。它无法找到那些jar文件。是否需要分别将每个jar文件添加到类路径?

22 个解决方案

#1


955  

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

使用Java 6或以后,classpath选项支持通配符。请注意以下几点:

  • Use straight quotes (")
  • 使用直引号(")
  • Use *, not *.jar
  • 不使用*,* . jar

Windows

窗户

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

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

Unix

Unix

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

“测试java - cp。jar:lib / *”my.package.MainClass

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):

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

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

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. 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.

类路径条目可以包含basename通配符*,这相当于使用.jar或.jar指定目录中所有文件的列表。例如,类路径条目foo/*指定名为foo的目录中的所有JAR文件。只包含*的类路径条目将扩展到当前目录中所有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中匹配类和JAR文件,可以使用foo;foo/*或foo/*;foo。所选择的顺序决定了foo中的类和资源是在foo中的JAR文件之前加载的,还是相反。

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

子目录没有递归地搜索。例如,foo/*只在foo/bar、foo/baz等中查找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文件的顺序没有被指定,并且在不同的平台上,甚至在同一台机器上的不同时刻都可能有所不同。一个构造良好的应用程序不应该依赖于任何特定的顺序。如果需要特定的顺序,那么可以在类路径中显式地枚举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. 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.

通配符的扩展是在早期,在程序的主方法调用之前完成的,而不是在类加载过程本身的后期。包含通配符的输入类路径的每个元素都被枚举命名目录中的JAR文件所生成的元素(可能为空)序列所取代。例如,如果目录foo包含a。jar,b。jar和c。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. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

类路径环境变量与-classpath(或-cp)命令行选项没有任何区别。也就是说,通配符在所有这些情况下都是受尊敬的。然而,类路径通配符在类路径的jar-manifest头中不被使用。

#2


198  

Under windows this works:

windows下如此:

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

and this does not work:

这并不管用:

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

notice the *.jar, so the * wildcard should be used alone.

注意*。jar,所以*通配符应该单独使用。


On Linux, the following works:

在Linux上,有以下工作:

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

The separators are colons instead of semicolons.

分隔符是冒号而不是分号。

#3


59  

We get around this problem by deploying a main jar file myapp.jar which contains a manifest (Manifest.mf) file specifying a classpath with the other required jars, which are then deployed alongside it. In this case, you only need to declare java -jar myapp.jar when running the code.

我们通过部署一个主jar文件myapp来解决这个问题。jar包含一个清单(manifest .mf)文件,该文件使用其他所需的jar指定类路径,然后在它旁边部署。在本例中,您只需声明java -jar myapp。运行代码时的jar。

So if you deploy the main jar into some directory, and then put the dependent jars into a lib folder beneath that, the manifest looks like:

因此,如果您将主jar部署到某个目录中,然后将相关的jar放到该目录下的lib文件夹中,那么清单将如下所示:

Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 1.0.1
Class-Path: lib/dep1.jar lib/dep2.jar

NB: this is platform-independent - we can use the same jars to launch on a UNIX server or on a Windows PC.

NB:这是独立于平台的——我们可以使用相同的jar在UNIX服务器或Windows PC上启动。

#4


36  

My solution on Ubuntu 10.04 using java-sun 1.6.0_24 having all jars in "lib" directory:

我的Ubuntu 10.04解决方案使用java-sun 1.6.0_24在“lib”目录中包含所有jar:

java -cp .:lib/* my.main.Class

If this fails, the following command should work (prints out all *.jars in lib directory to the classpath param)

如果失败,以下命令应该可以工作(打印出所有*。lib目录中的jar文件到类路径param)

java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). my.main.Class

#5


27  

Short answer: java -classpath lib/*:. my.package.Program

简短的回答:java -classpath lib/*:。my.package.Program

Oracle provides documentation on using wildcards in classpaths here for Java 6 and here for Java 7, under the section heading Understanding class path wildcards. (As I write this, the two pages contain the same information.) Here's a summary of the highlights:

Oracle提供了关于在类路径中使用通配符的文档,这里是Java 6,这里是Java 7,在“理解类路径通配符”一节的标题下。(在我写这篇文章时,这两页包含了相同的信息。)以下是一些亮点的总结:

  • In general, to include all of the JARs in a given directory, you can use the wildcard * (not *.jar).

    通常,要在给定的目录中包含所有jar,可以使用通配符*(而不是*.jar)。

  • The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

    通配符只匹配jar,而不是类文件;要在目录中获取所有类,只需在目录名称中结束类路径条目。

  • The above two options can be combined to include all JAR and class files in a directory, and the usual classpath precedence rules apply. E.g. -cp /classes;/jars/*

    可以将上述两个选项组合在一起,以包含目录中的所有JAR和类文件,并应用通常的类路径优先级规则。例如- cp /类;/ / *

  • The wildcard will not search for JARs in subdirectories.

    通配符不会在子目录中搜索jar。

  • The above bullet points are true if you use the CLASSPATH system property or the -cp or -classpath command line flags. However, if you use the Class-Path JAR manifest header (as you might do with an ant build file), wildcards will not be honored.

    如果您使用类路径系统属性或-cp或-classpath命令行标志,上述要点是正确的。但是,如果使用类路径JAR清单头(就像使用ant构建文件那样),通配符将不会被授予。

Yes, my first link is the same one provided in the top-scoring answer (which I have no hope of overtaking), but that answer doesn't provide much explanation beyond the link. Since that sort of behavior is discouraged on Stack Overflow these days, I thought I'd expand on it.

是的,我的第一个链接是同一个人提供的最高评分的答案(我没有希望超车),但是这个答案没有提供太多的解释。既然这种行为在Stack Overflow上是不受欢迎的,我想我应该扩展一下。

#6


27  

For me this works in windows .

对我来说,这在windows中是可行的。

java -cp "/lib/*;" sample

For linux

linux

java -cp "/lib/*:" sample

I am using Java 6

我使用的是Java 6。

#7


21  

You can try java -Djava.ext.dirs=jarDirectory http://docs.oracle.com/javase/6/docs/technotes/guides/extensions/spec.html

您可以尝试java -Djava.ext。dirs = jarDirectory http://docs.oracle.com/javase/6/docs/technotes/guides/extensions/spec.html

Directory for external jars when running java

运行java时用于外部jar的目录

#8


20  

Correct:

正确的:

java -classpath "lib/*:." my.package.Program

Incorrect:

不正确的:

java -classpath "lib/a*.jar:." my.package.Program
java -classpath "lib/a*:."     my.package.Program
java -classpath "lib/*.jar:."  my.package.Program
java -classpath  lib/*:.       my.package.Program

#9


12  

Windows:
java -cp file.jar;dir/* my.app.ClassName

java -cp文件.jar;dir/* my.app.ClassName

Linux:
java -cp file.jar:dir/* my.app.ClassName

Linux:java - cp文件。jar:dir / * my.app.ClassName

Remind:
- Windows path separator is ";"
- Linux path separator is ":"
- In Windows if cp argument does not contains white space, the "quotes" is optional

提醒:- Windows路径分隔符是“;”- Linux路径分隔符是":" -在Windows中,如果cp参数不包含空格,“引号”是可选的

#10


8  

If you really need to specify all the .jar files dynamically you could use shell scripts, or Apache Ant. There's a commons project called Commons Launcher which basically lets you specify your startup script as an ant build file (if you see what I mean).

如果您确实需要动态地指定所有.jar文件,您可以使用shell脚本或Apache Ant。有一个通用项目叫做commons Launcher,它可以让你把你的启动脚本指定为ant构建文件(如果你明白我的意思的话)。

Then, you can specify something like:

然后,您可以指定如下内容:

<path id="base.class.path">
    <pathelement path="${resources.dir}"/>
    <fileset dir="${extensions.dir}" includes="*.jar" />
    <fileset dir="${lib.dir}" includes="*.jar"/>
</path>

In your launch build file, which will launch your application with the correct classpath.

在启动构建文件中,该文件将使用正确的类路径启动应用程序。

#11


8  

If you are using Java 6, then you can use wildcards in the classpath.

如果您正在使用Java 6,那么您可以在类路径中使用通配符。

Now it is possible to use wildcards in classpath definition:

现在可以在类路径定义中使用通配符:

javac -cp libs/* -verbose -encoding UTF-8 src/mypackage/*.java  -d build/classes

Ref: http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

裁判:http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

#12


7  

Please note that wildcard expansion is broken for Java 7 on Windows.

请注意,Windows上的Java 7的通配符扩展被破坏了。

Check out this * issue for more information.

查看这个*问题了解更多信息。

The workaround is to put a semicolon right after the wildcard. java -cp "somewhere/*;"

解决方法是在通配符后面加上分号。java - cp“地方/ *”;

#13


4  

To whom it may concern,

致有关人士:

I found this strange behaviour on Windows under an MSYS/MinGW shell.

我在MSYS/MinGW外壳下的窗户上发现了这种奇怪的行为。

Works:

工作原理:

$ javac -cp '.;c:\Programs\COMSOL44\plugins\*' Reclaim.java

Doesn't work:

不工作:

$ javac -cp 'c:\Programs\COMSOL44\plugins\*' Reclaim.java
javac: invalid flag: c:\Programs\COMSOL44\plugins\com.comsol.aco_1.0.0.jar
Usage: javac <options> <source files>
use -help for a list of possible options

I am quite sure that the wildcard is not expanded by the shell, because e.g.

我确信通配符不是由shell展开的,因为

$ echo './*'
./*

(Tried it with another program too, rather than the built-in echo, with the same result.)

(我也尝试了另一个程序,而不是内置echo,结果也是一样的。)

I believe that it's javac which is trying to expand it, and it behaves differently whether there is a semicolon in the argument or not. First, it may be trying to expand all arguments that look like paths. And only then it would parse them, with -cp taking only the following token. (Note that com.comsol.aco_1.0.0.jar is the second JAR in that directory.) That's all a guess.

我相信是javac在尝试扩展它,无论参数中是否有分号,它的行为都是不同的。首先,它可能试图扩展所有看起来像路径的参数。只有这样,它才能解析它们,-cp只使用以下标记。(注意com.comsol.aco_1.0.0.jar是该目录中的第二个JAR。)这都是一种猜测。

This is

这是

$ javac -version
javac 1.7.0

#14


3  

All the above solutions work great if you develop and run the Java application outside any IDE like Eclipse or Netbeans.

如果您在任何IDE(如Eclipse或Netbeans)之外开发和运行Java应用程序,那么上述所有解决方案都非常有效。

If you are on Windows 7 and used Eclipse IDE for Development in Java, you might run into issues if using Command Prompt to run the class files built inside Eclipse.

如果您使用的是Windows 7,并且使用Eclipse IDE进行Java开发,那么如果使用命令提示符运行Eclipse内构建的类文件,您可能会遇到问题。

E.g. Your source code in Eclipse is having the following package hierarchy: edu.sjsu.myapp.Main.java

例如,Eclipse中的源代码具有以下包层次结构:edu.sjsu.myapp.Main.java

You have json.jar as an external dependency for the Main.java

你有json。jar作为Main.java的外部依赖项

When you try running Main.java from within Eclipse, it will run without any issues.

当你尝试运行Main时。java在Eclipse中运行时不会出现任何问题。

But when you try running this using Command Prompt after compiling Main.java in Eclipse, it will shoot some weird errors saying "ClassNotDef Error blah blah".

但是当您尝试在编译Main之后使用命令提示符运行这个命令时。在Eclipse中的java,它会抛出一些奇怪的错误,比如“ClassNotDef Error blah blah”。

I assume you are in the working directory of your source code !!

我假设您在您的源代码的工作目录中!

Use the following syntax to run it from command prompt:

使用以下语法从命令提示符运行:

  1. javac -cp ".;json.jar" Main.java

    javac - cp“json。jar”Main.java

  2. java -cp ".;json.jar" edu.sjsu.myapp.Main

    java - cp”。json。jar”edu.sjsu.myapp.Main

    [Don't miss the . above]

    (不要错过。以上)

This is because you have placed the Main.java inside the package edu.sjsu.myapp and java.exe will look for the exact pattern.

这是因为你放置了主服务器。java在包内。sjsu。myapp和java。exe将查找确切的模式。

Hope it helps !!

希望它帮助! !

#15


3  

For windows quotes are required and ; should be used as separator. e.g.:

对于windows报价是必需的;应该用作分隔符。例如:

java -cp "target\\*;target\\dependency\\*" my.package.Main

#16


3  

Short Form: If your main is within a jar, you'll probably need an additional '-jar pathTo/yourJar/YourJarsName.jar ' explicitly declared to get it working (even though 'YourJarsName.jar' was on the classpath) (or, expressed to answer the original question that was asked 5 years ago: you don't need to redeclare each jar explicitly, but does seem, even with java6 you need to redeclare your own jar ...)

简短的形式:如果你的主体在一个jar中,你可能需要一个额外的-jar路径到/你的jar /你的jarsname。jar显式地声明要让它工作(即使是“YourJarsName”)。jar在类路径上)(或者,表示要回答5年前提出的最初问题:您不需要显式地重新声明每个jar,但是,即使使用java6,您也需要重新声明您自己的jar…)


Long Form: (I've made this explicit to the point that I hope even interlopers to java can make use of this)

Long Form:(我已经明确地指出,我希望即使是java的入侵者也能利用它)

Like many here I'm using eclipse to export jars: (File->Export-->'Runnable JAR File'). There are three options on 'Library handling' eclipse (Juno) offers:

像这里的许多人一样,我使用eclipse来导出JAR: (File-> export ->'Runnable JAR File')。“库处理”eclipse (Juno)有三个选项:

opt1: "Extract required libraries into generated JAR"
opt2: "Package required libraries into generated JAR"
opt3: "Copy required libraries into a sub-folder next to the generated JAR"

Typically I'd use opt2 (and opt1 was definitely breaking), however native code in one of the jars I'm using I discovered breaks with the handy "jarinjar" trick that eclipse leverages when you choose that option. Even after realizing I needed opt3, and then finding this * entry, it still took me some time to figure it out how to launch my main outside of eclipse, so here's what worked for me, as it's useful for others...

通常我会使用opt2(而且opt1肯定会破坏),但是我在使用的一个jar中发现,当您选择该选项时,eclipse利用的便利的“jarinjar”技巧会破坏本机代码。即使我意识到我需要opt3,然后找到这个*条目,我还是花了一些时间来弄清楚如何在eclipse外部启动我的主程序,所以这就是我的工作,因为它对其他人很有用……


If you named your jar: "fooBarTheJarFile.jar" and all is set to export to the dir: "/theFully/qualifiedPath/toYourChosenDir".

如果你把你的罐子命名为“fooBarTheJarFile”。jar和所有设置为导出到目录:“/ thefull /qualifiedPath/toYourChosenDir”。

(meaning the 'Export destination' field will read: '/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar' )

(意味着“导出目的地”字段将会是:'/ thecomplete /qualifiedPath/toYourChosenDir/fooBarTheJarFile。jar”)

After you hit finish, you'll find eclipse then puts all the libraries into a folder named 'fooBarTheJarFile_lib' within that export directory, giving you something like:

完成后,您将发现eclipse然后将所有的库放到该导出目录中名为“fooBarTheJarFile_lib”的文件夹中,给您一些类似的东西:

/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar01.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar02.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar03.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar04.jar

You can then launch from anywhere on your system with:

然后,您可以从您的系统上的任何位置启动:

java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*" -jar  /theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar   package.path_to.the_class_with.your_main.TheClassWithYourMain

(For Java Newbies: 'package.path_to.the_class_with.your_main' is the declared package-path that you'll find at the top of the 'TheClassWithYourMain.java' file that contains the 'main(String[] args){...}' that you wish to run from outside java)

(Java新手:“package.path_to.the_class_with。your_main是在“TheClassWithYourMain”顶部找到的声明包路径。包含'main(String[] args){…您希望从java之外运行的})


The pitfall to notice: is that having 'fooBarTheJarFile.jar' within the list of jars on your declared classpath is not enough. You need to explicitly declare '-jar', and redeclare the location of that jar.

我们要注意的一个缺陷是:有“fooBarTheJarFile”。在声明的类路径上的jar列表中还不够。您需要显式地声明“-jar”,并重新声明该jar的位置。

e.g. this breaks:

如这优惠:

 java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar;/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*"  somepackages.inside.yourJar.leadingToTheMain.TheClassWithYourMain

restated with relative paths:

用相对路径重申:

cd /theFully/qualifiedPath/toYourChosenDir/;
BREAKS:  java -cp "fooBarTheJarFile_lib/*"                                package.path_to.the_class_with.your_main.TheClassWithYourMain    
BREAKS:  java -cp ".;fooBarTheJarFile_lib/*"                              package.path_to.the_class_with.your_main.TheClassWithYourMain   
BREAKS:  java -cp ".;fooBarTheJarFile_lib/*"   -jar                       package.path_to.the_class_with.your_main.TheClassWithYourMain   
WORKS:   java -cp ".;fooBarTheJarFile_lib/*"   -jar  fooBarTheJarFile.jar package.path_to.the_class_with.your_main.TheClassWithYourMain   

(using java version "1.6.0_27"; via OpenJDK 64-Bit Server VM on ubuntu 12.04)

(使用java版本“1.6.0_27”;通过ubuntu 12.04上的OpenJDK 64位服务器VM)

#17


2  

The only way I know how is to do it individually, for example:

我所知道的唯一方法就是单独做,例如:

setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.

Hope that helps!

希望会有帮助!

#18


2  

class from wepapp:

从wepapp类:

  > mvn clean install

  > java -cp "webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/tool-jar-1.17.0-SNAPSHOT.jar;webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/*" com.xx.xx.util.EncryptorUtils param1 param2

#19


1  

Not a direct solution to being able to set /* to -cp but I hope you could use the following script to ease the situation a bit for dynamic class-paths and lib directories.

不能直接将/*设置为-cp,但是我希望您可以使用下面的脚本来简化动态类路径和lib目录的情况。

 libDir2Scan4jars="../test";cp=""; for j in `ls ${libDir2Scan4jars}/*.jar`; do if [ "$j" != "" ]; then cp=$cp:$j; fi; done; echo $cp| cut -c2-${#cp} > .tmpCP.tmp; export tmpCLASSPATH=`cat .tmpCP.tmp`; if [ "$tmpCLASSPATH" != "" ]; then echo .; echo "classpath set, you can now use  ~>         java -cp \$tmpCLASSPATH"; echo .; else echo .; echo "Error please check libDir2Scan4jars path"; echo .; fi; 

Scripted for Linux, could have a similar one for windows too. If proper directory is provided as input to the "libDir2Scan4jars"; the script will scan all the jars and create a classpath string and export it to a env variable "tmpCLASSPATH".

为Linux编写脚本,也可以为windows编写类似的脚本。如果提供了适当的目录作为“libdir2scan4jar”的输入;脚本将扫描所有jar并创建一个类路径字符串,并将其导出到env变量“tmpCLASSPATH”。

#20


0  

You need to add them all separately. Alternatively, if you really need to just specify a directory, you can unjar everything into one dir and add that to your classpath. I don't recommend this approach however as you risk bizarre problems in classpath versioning and unmanagability.

您需要分别添加它们。另外,如果您确实需要指定一个目录,那么可以将所有内容解压缩到一个目录中,并将其添加到类路径中。但是,我不推荐这种方法,因为您可能会在类路径版本控制和不可管理性方面遇到奇怪的问题。

#21


0  

Think of a jar file as the root of a directory structure. Yes, you need to add them all separately.

可以将jar文件看作目录结构的根文件。是的,你需要分别把它们加起来。

#22


0  

Set the classpath in a way suitable multiple jars and current directory's class files.

以适合多个jar和当前目录的类文件的方式设置类路径。

CLASSPATH=${ORACLE_HOME}/jdbc/lib/ojdbc6.jar:${ORACLE_HOME}/jdbc/lib/ojdbc14.jar:${ORACLE_HOME}/jdbc/lib/nls_charset12.jar; 
CLASSPATH=$CLASSPATH:/export/home/gs806e/tops/jconn2.jar:.;
export CLASSPATH

#1


955  

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

使用Java 6或以后,classpath选项支持通配符。请注意以下几点:

  • Use straight quotes (")
  • 使用直引号(")
  • Use *, not *.jar
  • 不使用*,* . jar

Windows

窗户

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

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

Unix

Unix

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

“测试java - cp。jar:lib / *”my.package.MainClass

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):

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

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

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. 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.

类路径条目可以包含basename通配符*,这相当于使用.jar或.jar指定目录中所有文件的列表。例如,类路径条目foo/*指定名为foo的目录中的所有JAR文件。只包含*的类路径条目将扩展到当前目录中所有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中匹配类和JAR文件,可以使用foo;foo/*或foo/*;foo。所选择的顺序决定了foo中的类和资源是在foo中的JAR文件之前加载的,还是相反。

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

子目录没有递归地搜索。例如,foo/*只在foo/bar、foo/baz等中查找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文件的顺序没有被指定,并且在不同的平台上,甚至在同一台机器上的不同时刻都可能有所不同。一个构造良好的应用程序不应该依赖于任何特定的顺序。如果需要特定的顺序,那么可以在类路径中显式地枚举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. 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.

通配符的扩展是在早期,在程序的主方法调用之前完成的,而不是在类加载过程本身的后期。包含通配符的输入类路径的每个元素都被枚举命名目录中的JAR文件所生成的元素(可能为空)序列所取代。例如,如果目录foo包含a。jar,b。jar和c。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. That is, wildcards are honored in all these cases. However, class path wildcards are not honored in the Class-Path jar-manifest header.

类路径环境变量与-classpath(或-cp)命令行选项没有任何区别。也就是说,通配符在所有这些情况下都是受尊敬的。然而,类路径通配符在类路径的jar-manifest头中不被使用。

#2


198  

Under windows this works:

windows下如此:

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

and this does not work:

这并不管用:

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

notice the *.jar, so the * wildcard should be used alone.

注意*。jar,所以*通配符应该单独使用。


On Linux, the following works:

在Linux上,有以下工作:

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

The separators are colons instead of semicolons.

分隔符是冒号而不是分号。

#3


59  

We get around this problem by deploying a main jar file myapp.jar which contains a manifest (Manifest.mf) file specifying a classpath with the other required jars, which are then deployed alongside it. In this case, you only need to declare java -jar myapp.jar when running the code.

我们通过部署一个主jar文件myapp来解决这个问题。jar包含一个清单(manifest .mf)文件,该文件使用其他所需的jar指定类路径,然后在它旁边部署。在本例中,您只需声明java -jar myapp。运行代码时的jar。

So if you deploy the main jar into some directory, and then put the dependent jars into a lib folder beneath that, the manifest looks like:

因此,如果您将主jar部署到某个目录中,然后将相关的jar放到该目录下的lib文件夹中,那么清单将如下所示:

Manifest-Version: 1.0
Implementation-Title: myapp
Implementation-Version: 1.0.1
Class-Path: lib/dep1.jar lib/dep2.jar

NB: this is platform-independent - we can use the same jars to launch on a UNIX server or on a Windows PC.

NB:这是独立于平台的——我们可以使用相同的jar在UNIX服务器或Windows PC上启动。

#4


36  

My solution on Ubuntu 10.04 using java-sun 1.6.0_24 having all jars in "lib" directory:

我的Ubuntu 10.04解决方案使用java-sun 1.6.0_24在“lib”目录中包含所有jar:

java -cp .:lib/* my.main.Class

If this fails, the following command should work (prints out all *.jars in lib directory to the classpath param)

如果失败,以下命令应该可以工作(打印出所有*。lib目录中的jar文件到类路径param)

java -cp $(for i in lib/*.jar ; do echo -n $i: ; done). my.main.Class

#5


27  

Short answer: java -classpath lib/*:. my.package.Program

简短的回答:java -classpath lib/*:。my.package.Program

Oracle provides documentation on using wildcards in classpaths here for Java 6 and here for Java 7, under the section heading Understanding class path wildcards. (As I write this, the two pages contain the same information.) Here's a summary of the highlights:

Oracle提供了关于在类路径中使用通配符的文档,这里是Java 6,这里是Java 7,在“理解类路径通配符”一节的标题下。(在我写这篇文章时,这两页包含了相同的信息。)以下是一些亮点的总结:

  • In general, to include all of the JARs in a given directory, you can use the wildcard * (not *.jar).

    通常,要在给定的目录中包含所有jar,可以使用通配符*(而不是*.jar)。

  • The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

    通配符只匹配jar,而不是类文件;要在目录中获取所有类,只需在目录名称中结束类路径条目。

  • The above two options can be combined to include all JAR and class files in a directory, and the usual classpath precedence rules apply. E.g. -cp /classes;/jars/*

    可以将上述两个选项组合在一起,以包含目录中的所有JAR和类文件,并应用通常的类路径优先级规则。例如- cp /类;/ / *

  • The wildcard will not search for JARs in subdirectories.

    通配符不会在子目录中搜索jar。

  • The above bullet points are true if you use the CLASSPATH system property or the -cp or -classpath command line flags. However, if you use the Class-Path JAR manifest header (as you might do with an ant build file), wildcards will not be honored.

    如果您使用类路径系统属性或-cp或-classpath命令行标志,上述要点是正确的。但是,如果使用类路径JAR清单头(就像使用ant构建文件那样),通配符将不会被授予。

Yes, my first link is the same one provided in the top-scoring answer (which I have no hope of overtaking), but that answer doesn't provide much explanation beyond the link. Since that sort of behavior is discouraged on Stack Overflow these days, I thought I'd expand on it.

是的,我的第一个链接是同一个人提供的最高评分的答案(我没有希望超车),但是这个答案没有提供太多的解释。既然这种行为在Stack Overflow上是不受欢迎的,我想我应该扩展一下。

#6


27  

For me this works in windows .

对我来说,这在windows中是可行的。

java -cp "/lib/*;" sample

For linux

linux

java -cp "/lib/*:" sample

I am using Java 6

我使用的是Java 6。

#7


21  

You can try java -Djava.ext.dirs=jarDirectory http://docs.oracle.com/javase/6/docs/technotes/guides/extensions/spec.html

您可以尝试java -Djava.ext。dirs = jarDirectory http://docs.oracle.com/javase/6/docs/technotes/guides/extensions/spec.html

Directory for external jars when running java

运行java时用于外部jar的目录

#8


20  

Correct:

正确的:

java -classpath "lib/*:." my.package.Program

Incorrect:

不正确的:

java -classpath "lib/a*.jar:." my.package.Program
java -classpath "lib/a*:."     my.package.Program
java -classpath "lib/*.jar:."  my.package.Program
java -classpath  lib/*:.       my.package.Program

#9


12  

Windows:
java -cp file.jar;dir/* my.app.ClassName

java -cp文件.jar;dir/* my.app.ClassName

Linux:
java -cp file.jar:dir/* my.app.ClassName

Linux:java - cp文件。jar:dir / * my.app.ClassName

Remind:
- Windows path separator is ";"
- Linux path separator is ":"
- In Windows if cp argument does not contains white space, the "quotes" is optional

提醒:- Windows路径分隔符是“;”- Linux路径分隔符是":" -在Windows中,如果cp参数不包含空格,“引号”是可选的

#10


8  

If you really need to specify all the .jar files dynamically you could use shell scripts, or Apache Ant. There's a commons project called Commons Launcher which basically lets you specify your startup script as an ant build file (if you see what I mean).

如果您确实需要动态地指定所有.jar文件,您可以使用shell脚本或Apache Ant。有一个通用项目叫做commons Launcher,它可以让你把你的启动脚本指定为ant构建文件(如果你明白我的意思的话)。

Then, you can specify something like:

然后,您可以指定如下内容:

<path id="base.class.path">
    <pathelement path="${resources.dir}"/>
    <fileset dir="${extensions.dir}" includes="*.jar" />
    <fileset dir="${lib.dir}" includes="*.jar"/>
</path>

In your launch build file, which will launch your application with the correct classpath.

在启动构建文件中,该文件将使用正确的类路径启动应用程序。

#11


8  

If you are using Java 6, then you can use wildcards in the classpath.

如果您正在使用Java 6,那么您可以在类路径中使用通配符。

Now it is possible to use wildcards in classpath definition:

现在可以在类路径定义中使用通配符:

javac -cp libs/* -verbose -encoding UTF-8 src/mypackage/*.java  -d build/classes

Ref: http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

裁判:http://www.rekk.de/bloggy/2008/add-all-jars-in-a-directory-to-classpath-with-java-se-6-using-wildcards/

#12


7  

Please note that wildcard expansion is broken for Java 7 on Windows.

请注意,Windows上的Java 7的通配符扩展被破坏了。

Check out this * issue for more information.

查看这个*问题了解更多信息。

The workaround is to put a semicolon right after the wildcard. java -cp "somewhere/*;"

解决方法是在通配符后面加上分号。java - cp“地方/ *”;

#13


4  

To whom it may concern,

致有关人士:

I found this strange behaviour on Windows under an MSYS/MinGW shell.

我在MSYS/MinGW外壳下的窗户上发现了这种奇怪的行为。

Works:

工作原理:

$ javac -cp '.;c:\Programs\COMSOL44\plugins\*' Reclaim.java

Doesn't work:

不工作:

$ javac -cp 'c:\Programs\COMSOL44\plugins\*' Reclaim.java
javac: invalid flag: c:\Programs\COMSOL44\plugins\com.comsol.aco_1.0.0.jar
Usage: javac <options> <source files>
use -help for a list of possible options

I am quite sure that the wildcard is not expanded by the shell, because e.g.

我确信通配符不是由shell展开的,因为

$ echo './*'
./*

(Tried it with another program too, rather than the built-in echo, with the same result.)

(我也尝试了另一个程序,而不是内置echo,结果也是一样的。)

I believe that it's javac which is trying to expand it, and it behaves differently whether there is a semicolon in the argument or not. First, it may be trying to expand all arguments that look like paths. And only then it would parse them, with -cp taking only the following token. (Note that com.comsol.aco_1.0.0.jar is the second JAR in that directory.) That's all a guess.

我相信是javac在尝试扩展它,无论参数中是否有分号,它的行为都是不同的。首先,它可能试图扩展所有看起来像路径的参数。只有这样,它才能解析它们,-cp只使用以下标记。(注意com.comsol.aco_1.0.0.jar是该目录中的第二个JAR。)这都是一种猜测。

This is

这是

$ javac -version
javac 1.7.0

#14


3  

All the above solutions work great if you develop and run the Java application outside any IDE like Eclipse or Netbeans.

如果您在任何IDE(如Eclipse或Netbeans)之外开发和运行Java应用程序,那么上述所有解决方案都非常有效。

If you are on Windows 7 and used Eclipse IDE for Development in Java, you might run into issues if using Command Prompt to run the class files built inside Eclipse.

如果您使用的是Windows 7,并且使用Eclipse IDE进行Java开发,那么如果使用命令提示符运行Eclipse内构建的类文件,您可能会遇到问题。

E.g. Your source code in Eclipse is having the following package hierarchy: edu.sjsu.myapp.Main.java

例如,Eclipse中的源代码具有以下包层次结构:edu.sjsu.myapp.Main.java

You have json.jar as an external dependency for the Main.java

你有json。jar作为Main.java的外部依赖项

When you try running Main.java from within Eclipse, it will run without any issues.

当你尝试运行Main时。java在Eclipse中运行时不会出现任何问题。

But when you try running this using Command Prompt after compiling Main.java in Eclipse, it will shoot some weird errors saying "ClassNotDef Error blah blah".

但是当您尝试在编译Main之后使用命令提示符运行这个命令时。在Eclipse中的java,它会抛出一些奇怪的错误,比如“ClassNotDef Error blah blah”。

I assume you are in the working directory of your source code !!

我假设您在您的源代码的工作目录中!

Use the following syntax to run it from command prompt:

使用以下语法从命令提示符运行:

  1. javac -cp ".;json.jar" Main.java

    javac - cp“json。jar”Main.java

  2. java -cp ".;json.jar" edu.sjsu.myapp.Main

    java - cp”。json。jar”edu.sjsu.myapp.Main

    [Don't miss the . above]

    (不要错过。以上)

This is because you have placed the Main.java inside the package edu.sjsu.myapp and java.exe will look for the exact pattern.

这是因为你放置了主服务器。java在包内。sjsu。myapp和java。exe将查找确切的模式。

Hope it helps !!

希望它帮助! !

#15


3  

For windows quotes are required and ; should be used as separator. e.g.:

对于windows报价是必需的;应该用作分隔符。例如:

java -cp "target\\*;target\\dependency\\*" my.package.Main

#16


3  

Short Form: If your main is within a jar, you'll probably need an additional '-jar pathTo/yourJar/YourJarsName.jar ' explicitly declared to get it working (even though 'YourJarsName.jar' was on the classpath) (or, expressed to answer the original question that was asked 5 years ago: you don't need to redeclare each jar explicitly, but does seem, even with java6 you need to redeclare your own jar ...)

简短的形式:如果你的主体在一个jar中,你可能需要一个额外的-jar路径到/你的jar /你的jarsname。jar显式地声明要让它工作(即使是“YourJarsName”)。jar在类路径上)(或者,表示要回答5年前提出的最初问题:您不需要显式地重新声明每个jar,但是,即使使用java6,您也需要重新声明您自己的jar…)


Long Form: (I've made this explicit to the point that I hope even interlopers to java can make use of this)

Long Form:(我已经明确地指出,我希望即使是java的入侵者也能利用它)

Like many here I'm using eclipse to export jars: (File->Export-->'Runnable JAR File'). There are three options on 'Library handling' eclipse (Juno) offers:

像这里的许多人一样,我使用eclipse来导出JAR: (File-> export ->'Runnable JAR File')。“库处理”eclipse (Juno)有三个选项:

opt1: "Extract required libraries into generated JAR"
opt2: "Package required libraries into generated JAR"
opt3: "Copy required libraries into a sub-folder next to the generated JAR"

Typically I'd use opt2 (and opt1 was definitely breaking), however native code in one of the jars I'm using I discovered breaks with the handy "jarinjar" trick that eclipse leverages when you choose that option. Even after realizing I needed opt3, and then finding this * entry, it still took me some time to figure it out how to launch my main outside of eclipse, so here's what worked for me, as it's useful for others...

通常我会使用opt2(而且opt1肯定会破坏),但是我在使用的一个jar中发现,当您选择该选项时,eclipse利用的便利的“jarinjar”技巧会破坏本机代码。即使我意识到我需要opt3,然后找到这个*条目,我还是花了一些时间来弄清楚如何在eclipse外部启动我的主程序,所以这就是我的工作,因为它对其他人很有用……


If you named your jar: "fooBarTheJarFile.jar" and all is set to export to the dir: "/theFully/qualifiedPath/toYourChosenDir".

如果你把你的罐子命名为“fooBarTheJarFile”。jar和所有设置为导出到目录:“/ thefull /qualifiedPath/toYourChosenDir”。

(meaning the 'Export destination' field will read: '/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar' )

(意味着“导出目的地”字段将会是:'/ thecomplete /qualifiedPath/toYourChosenDir/fooBarTheJarFile。jar”)

After you hit finish, you'll find eclipse then puts all the libraries into a folder named 'fooBarTheJarFile_lib' within that export directory, giving you something like:

完成后,您将发现eclipse然后将所有的库放到该导出目录中名为“fooBarTheJarFile_lib”的文件夹中,给您一些类似的东西:

/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar01.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar02.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar03.jar
/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/SomeOtherJar04.jar

You can then launch from anywhere on your system with:

然后,您可以从您的系统上的任何位置启动:

java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*" -jar  /theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar   package.path_to.the_class_with.your_main.TheClassWithYourMain

(For Java Newbies: 'package.path_to.the_class_with.your_main' is the declared package-path that you'll find at the top of the 'TheClassWithYourMain.java' file that contains the 'main(String[] args){...}' that you wish to run from outside java)

(Java新手:“package.path_to.the_class_with。your_main是在“TheClassWithYourMain”顶部找到的声明包路径。包含'main(String[] args){…您希望从java之外运行的})


The pitfall to notice: is that having 'fooBarTheJarFile.jar' within the list of jars on your declared classpath is not enough. You need to explicitly declare '-jar', and redeclare the location of that jar.

我们要注意的一个缺陷是:有“fooBarTheJarFile”。在声明的类路径上的jar列表中还不够。您需要显式地声明“-jar”,并重新声明该jar的位置。

e.g. this breaks:

如这优惠:

 java -classpath "/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile.jar;/theFully/qualifiedPath/toYourChosenDir/fooBarTheJarFile_lib/*"  somepackages.inside.yourJar.leadingToTheMain.TheClassWithYourMain

restated with relative paths:

用相对路径重申:

cd /theFully/qualifiedPath/toYourChosenDir/;
BREAKS:  java -cp "fooBarTheJarFile_lib/*"                                package.path_to.the_class_with.your_main.TheClassWithYourMain    
BREAKS:  java -cp ".;fooBarTheJarFile_lib/*"                              package.path_to.the_class_with.your_main.TheClassWithYourMain   
BREAKS:  java -cp ".;fooBarTheJarFile_lib/*"   -jar                       package.path_to.the_class_with.your_main.TheClassWithYourMain   
WORKS:   java -cp ".;fooBarTheJarFile_lib/*"   -jar  fooBarTheJarFile.jar package.path_to.the_class_with.your_main.TheClassWithYourMain   

(using java version "1.6.0_27"; via OpenJDK 64-Bit Server VM on ubuntu 12.04)

(使用java版本“1.6.0_27”;通过ubuntu 12.04上的OpenJDK 64位服务器VM)

#17


2  

The only way I know how is to do it individually, for example:

我所知道的唯一方法就是单独做,例如:

setenv CLASSPATH /User/username/newfolder/jarfile.jar:jarfile2.jar:jarfile3.jar:.

Hope that helps!

希望会有帮助!

#18


2  

class from wepapp:

从wepapp类:

  > mvn clean install

  > java -cp "webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/tool-jar-1.17.0-SNAPSHOT.jar;webapp/target/webapp-1.17.0-SNAPSHOT/WEB-INF/lib/*" com.xx.xx.util.EncryptorUtils param1 param2

#19


1  

Not a direct solution to being able to set /* to -cp but I hope you could use the following script to ease the situation a bit for dynamic class-paths and lib directories.

不能直接将/*设置为-cp,但是我希望您可以使用下面的脚本来简化动态类路径和lib目录的情况。

 libDir2Scan4jars="../test";cp=""; for j in `ls ${libDir2Scan4jars}/*.jar`; do if [ "$j" != "" ]; then cp=$cp:$j; fi; done; echo $cp| cut -c2-${#cp} > .tmpCP.tmp; export tmpCLASSPATH=`cat .tmpCP.tmp`; if [ "$tmpCLASSPATH" != "" ]; then echo .; echo "classpath set, you can now use  ~>         java -cp \$tmpCLASSPATH"; echo .; else echo .; echo "Error please check libDir2Scan4jars path"; echo .; fi; 

Scripted for Linux, could have a similar one for windows too. If proper directory is provided as input to the "libDir2Scan4jars"; the script will scan all the jars and create a classpath string and export it to a env variable "tmpCLASSPATH".

为Linux编写脚本,也可以为windows编写类似的脚本。如果提供了适当的目录作为“libdir2scan4jar”的输入;脚本将扫描所有jar并创建一个类路径字符串,并将其导出到env变量“tmpCLASSPATH”。

#20


0  

You need to add them all separately. Alternatively, if you really need to just specify a directory, you can unjar everything into one dir and add that to your classpath. I don't recommend this approach however as you risk bizarre problems in classpath versioning and unmanagability.

您需要分别添加它们。另外,如果您确实需要指定一个目录,那么可以将所有内容解压缩到一个目录中,并将其添加到类路径中。但是,我不推荐这种方法,因为您可能会在类路径版本控制和不可管理性方面遇到奇怪的问题。

#21


0  

Think of a jar file as the root of a directory structure. Yes, you need to add them all separately.

可以将jar文件看作目录结构的根文件。是的,你需要分别把它们加起来。

#22


0  

Set the classpath in a way suitable multiple jars and current directory's class files.

以适合多个jar和当前目录的类文件的方式设置类路径。

CLASSPATH=${ORACLE_HOME}/jdbc/lib/ojdbc6.jar:${ORACLE_HOME}/jdbc/lib/ojdbc14.jar:${ORACLE_HOME}/jdbc/lib/nls_charset12.jar; 
CLASSPATH=$CLASSPATH:/export/home/gs806e/tops/jconn2.jar:.;
export CLASSPATH