java 我应该如何设置 CLASSPATH?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2017833/
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 should I set CLASSPATH?
提问by phoenies
I did this before:
我以前这样做过:
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar"
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar"
But today an article says I should do this:
但是今天一篇文章说我应该这样做:
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib"
CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib"
If I do so, will it search all the jar files in lib? So it's probably a shorter way?
如果我这样做,它会搜索 lib 中的所有 jar 文件吗?所以这可能是一种更短的方式?
回答by Cesar
Since you are using JDK6, you can use classpath wildcards: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" will match all JARS inside lib/
由于您使用的是 JDK6,您可以使用类路径通配符: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" 将匹配 lib/ 中的所有 JARS
Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.htmlthere's a section called "Understanding class path wildcards"
查看http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html有一个名为“了解类路径通配符”的部分
回答by duffymo
I think having a CLASSPATH environment variable is wrong for all but the easiest of "Hello, World" tutorials.
我认为拥有 CLASSPATH 环境变量对于除了最简单的“Hello, World”教程之外的所有教程都是错误的。
The right way is to set the CLASSPATH for every project when you compile and run. Every project is likely to be different, so this makes perfect sense.
正确的方法是在编译和运行时为每个项目设置CLASSPATH。每个项目可能都不同,所以这是完全有道理的。
IDEs ignore CLASSPATH environment settings; so do all Java EE app servers. It's a relic of Java 1.0. I don't have CLASSPATH set on any machine that I work on.
IDE 忽略 CLASSPATH 环境设置;所有 Java EE 应用服务器也是如此。它是 Java 1.0 的遗物。我没有在我工作的任何机器上设置 CLASSPATH。
Learn to script it for the command line. Or use Ant. You'll be glad you did.
学习为命令行编写脚本。或者使用蚂蚁。你会很高兴你做到了。
回答by Jim L
Yes, it will search all jar files in lib if you do it the second way. It's pretty odd to see class path being set as specifically as in the first one. I suppose on a server where you wanted to be sure what jars were being loaded, that might be one way to restrict them, but you might run into issues with how long it can be if you had several jars.
是的,如果您采用第二种方式,它将搜索 lib 中的所有 jar 文件。看到类路径像第一个一样具体设置,这很奇怪。我想在您想确定正在加载哪些 jar 的服务器上,这可能是限制它们的一种方法,但是如果您有多个 jar,您可能会遇到可以加载多长时间的问题。
回答by akf
Jar files need to be specified by name in the Classpath variable. One thing to note is that the commandline -classpathparam is more versatile than the environment variable, as it allows you to set a classpath per application.
Jar 文件需要在 Classpath 变量中按名称指定。需要注意的一件事是命令行-classpath参数比环境变量更通用,因为它允许您为每个应用程序设置类路径。
回答by Nate
In Java 1.6+ you can set the classpath to a directory followed by /* to load all JAR files in that directory. Not just the directory name though - that's for loading class files in that directory and subdirectories.
在 Java 1.6+ 中,您可以将类路径设置为一个目录,后跟 /* 以加载该目录中的所有 JAR 文件。不只是目录名称 - 这是用于加载该目录和子目录中的类文件。

