我应该如何设置CLASSPATH?
我之前这样做过:
CLASSPATH=".:/home/phenies/jdk1.6.0_17/lib/tools.jar:/home/phenies/jdk1.6.0_17/lib/dt.jar"
但今天一篇文章说我应该这样做:
CLASSPATH=".:/home/phenies/jdk1.6.0_17/lib"
如果我这样做,它会搜索lib中的所有jar文件吗?那么这可能是一条更短的路?
I did this before:
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"
If I do so, will it search all the jar files in lib? So it's probably a shorter way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于您使用的是 JDK6,因此可以使用类路径通配符: CLASSPATH=".:/home/phenies/jdk1.6.0_17/lib/*" 将匹配 lib/ 内的所有 JARS
查看 http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath .html 有一个名为“理解类路径通配符”的部分
Since you are using JDK6, you can use classpath wildcards: CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/*" will match all JARS inside lib/
Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html there's a section called "Understanding class path wildcards"
我认为除了最简单的“Hello,World”教程之外,拥有 CLASSPATH 环境变量对于所有教程都是错误的。
正确的方法是在编译和运行时为每个项目设置CLASSPATH。每个项目都可能不同,所以这是完全有道理的。
IDE 忽略 CLASSPATH 环境设置;所有 Java EE 应用服务器也是如此。它是 Java 1.0 的遗物。我没有在我工作的任何机器上设置 CLASSPATH。
学习为命令行编写脚本。或者使用蚂蚁。你会很高兴你这么做了。
I think having a CLASSPATH environment variable is wrong for all but the easiest of "Hello, World" tutorials.
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.
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.
Learn to script it for the command line. Or use Ant. You'll be glad you did.
是的,如果你用第二种方法,它会搜索lib中的所有jar文件。看到类路径被像第一个那样具体设置是很奇怪的。我想在您想要确定正在加载哪些 jar 的服务器上,这可能是限制它们的一种方法,但是如果您有多个 jar,您可能会遇到加载时间的问题。
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.
Jar 文件需要通过 Classpath 变量中的名称指定。需要注意的一件事是,命令行
-classpath
参数比环境变量更通用,因为它允许您为每个应用程序设置类路径。Jar files need to be specified by name in the Classpath variable. One thing to note is that the commandline
-classpath
param is more versatile than the environment variable, as it allows you to set a classpath per application.在 Java 1.6+ 中,您可以将类路径设置为后跟 /* 的目录,以加载该目录中的所有 JAR 文件。不仅仅是目录名称 - 这是用于加载该目录和子目录中的类文件。
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.