Java 如何在类路径中使用通配符添加多个jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1237093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a wildcard in the classpath to add multiple jars?
提问by paulbullard
I have been using so many 3rd party libraries(jar files) that my CLASSPATH is completely messed up as i have to include the path for every single jar file that i use.
我一直在使用如此多的 3rd 方库(jar 文件),以至于我的 CLASSPATH 完全搞砸了,因为我必须包含我使用的每个 jar 文件的路径。
I've been wondering if there is a way to include all the jar files in a folder using wildcard(*) operator (like *.jar). But it seems to be not working. Is there any other way that can shorten the CLASSPATH that currently looks like an essay ;) on my PC?.
我一直想知道是否有一种方法可以使用通配符(*)运算符(如 *.jar)将所有 jar 文件包含在文件夹中。但它似乎不起作用。有没有其他方法可以缩短目前看起来像一篇文章 ;) 在我的 PC 上的 CLASSPATH?
采纳答案by Itay Maman
From: http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
来自:http: //java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html
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.
类路径条目可以包含基本名称通配符
*
,这被认为等同于指定目录中所有文件的列表,扩展名为 .jar 或 .JAR。例如,类路径条目foo/*
指定名为 foo 的目录中的所有 JAR 文件。一个仅由 组成的类路径条目*
扩展为当前目录中所有 jar 文件的列表。
This should work in Java6, not sure about Java5
这应该适用于 Java6,不确定 Java5
(If it seems it does not work as expected, try putting quotes. eg: "foo/*"
)
(如果它似乎它不能按预期工作,尝试把引号例如:。 "foo/*"
)
回答by Stephen C
Basename wild cards were introduced in Java 6; i.e. "foo/*" means all ".jar" files in the "foo" directory.
Java 6 中引入了 Basename 通配符;即“foo/*”表示“foo”目录中的所有“.jar”文件。
In earlier versions of Java that do not support wildcard classpaths, I have resorted to using a shell wrapper script to assemble a Classpath by 'globbing' a pattern and mangling the results to insert ':' characters at the appropriate points. This would be hard to do in a BAT file ...
在不支持通配符类路径的早期 Java 版本中,我已经使用 shell 包装器脚本来组装类路径,方法是“通配”一个模式并修改结果以在适当的点插入 ':' 字符。这在 BAT 文件中很难做到......
回答by duffymo
If you mean that you have an environment variable named CLASSPATH, I'd say that's your mistake. I don't have such a thing on any machine with which I develop Java. CLASSPATH is so tied to a particular project that it's impossible to have a single, correct CLASSPATH that works for all.
如果你的意思是你有一个名为 CLASSPATH 的环境变量,我会说那是你的错误。我在开发 Java 的任何机器上都没有这样的东西。CLASSPATH 与特定项目如此紧密地联系在一起,以至于不可能有一个适用于所有人的单一、正确的 CLASSPATH。
I set CLASSPATH for each project using either an IDE or Ant. I do a lot of web development, so each WAR and EAR uses their own CLASSPATH.
我使用 IDE 或 Ant 为每个项目设置 CLASSPATH。我做了很多 Web 开发,所以每个 WAR 和 EAR 都使用自己的 CLASSPATH。
It's ignored by IDEs and app servers. Why do you have it? I'd recommend deleting it.
它被 IDE 和应用服务器忽略。你为什么拥有它?我建议删除它。
回答by Chris Milburn
This works on Windows:
这适用于 Windows:
java -cp "lib/*" %MAINCLASS%
where %MAINCLASS%
of course is the class containing your main method.
哪里%MAINCLASS%
当然是包含您的主要方法的类。
Alternatively:
或者:
java -cp "lib/*" -jar %MAINJAR%
where %MAINJAR%
is the jar file to launch via its internal manifest.
%MAINJAR%
要通过其内部清单启动的 jar 文件在哪里。