如何在类路径中运行带有 jar 的 java 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6409510/
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 run a java class with a jar in the classpath?
提问by amara
So, I can do this very well:
所以,我可以很好地做到这一点:
java mypackage.MyClass
if ./mypackage/MyClass.class
exists. I can also happily do this:
如果./mypackage/MyClass.class
存在。我也可以愉快地这样做:
java -cp myjar.jar mypackage.MyClass
if the class file exists in the appropriate part of the jar. Easy stuff. But I can't for the life of me manage to do something like this:
如果类文件存在于 jar 的适当部分。容易的东西。但我一生都无法做到这样的事情:
java -cp utilities.jar mypackage.MyClass
where ./mypackage/MyClass.class
exists, and where ./utilities.jar
exists (not containing MyClass, of course).
哪里./mypackage/MyClass.class
存在,哪里./utilities.jar
存在(当然不包含 MyClass)。
Am I about to feel stupid?
我会觉得自己很傻吗?
采纳答案by Jon Skeet
Possibly :)
可能:)
# On Unix
java -cp utilities.jar:. mypackage.MyClass
# On Windows
java -cp utilities.jar;. mypackage.MyClass
Basically that's just including .
(the current directory) on the classpath as well as the jar file.
基本上,这只是.
在类路径和 jar 文件中包含(当前目录)。
回答by duffymo
Try this if you're on Windows:
如果你在 Windows 上试试这个:
java -cp .;utilities.jar mypackage.MyClass
Or this if you're on Linux:
或者,如果您使用的是 Linux:
java -cp .:utilities.jar mypackage.MyClass
The current directory is not in the CLASSPATH by default when you specify a value for -cp
.
默认情况下,当您为 指定值时,当前目录不在 CLASSPATH 中-cp
。
回答by YODA
You should include the mypackage.MyClass into the CLASSPATH or the -cp parameter. For example:
您应该将 mypackage.MyClass 包含到 CLASSPATH 或 -cp 参数中。例如:
java -cp utilities.jar;myjar.jar mypackage.MyClass
The path separator is ; on windows and : on Unix/Linux
路径分隔符是; 在 Windows 上和:在 Unix/Linux 上