简体   繁体   English

我该如何设置CLASSPATH?

[英]How should I set CLASSPATH?

I did this before: 我以前这样做过:

CLASSPATH=".:/home/phoenies/jdk1.6.0_17/lib/tools.jar:/home/phoenies/jdk1.6.0_17/lib/dt.jar" CLASSPATH = “:/家庭/ 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 = “:/家庭/ phoenies / jdk1.6.0_17 / lib目录”

If I do so, will it search all the jar files in lib? 如果我这样做,它会搜索lib中的所有jar文件吗? So it's probably a shorter way? 所以它可能是一个更短的方式?

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通配符:CLASSPATH =“。:/ home / phoenies / jdk1.6.0_17 / lib / *”将匹配lib /中的所有JARS

Check out http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html there's a section called "Understanding class path wildcards" 查看http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html,其中有一节名为“了解类路径通配符”

I think having a CLASSPATH environment variable is wrong for all but the easiest of "Hello, World" tutorials. 我认为除了最简单的“Hello,World”教程外,拥有CLASSPATH环境变量是错误的。

The right way is to set the CLASSPATH for every project when you compile and run. 正确的方法是在编译和运行时为每个项目设置CLASSPATH。 Every project is likely to be different, so this makes perfect sense. 每个项目都可能有所不同,所以这很有道理。

IDEs ignore CLASSPATH environment settings; IDE忽略CLASSPATH环境设置; so do all Java EE app servers. 所有Java EE应用服务器也是如此。 It's a relic of Java 1.0. 它是Java 1.0的遗留物。 I don't have CLASSPATH set on any machine that I work on. 我没有在我工作的任何机器上设置CLASSPATH。

Learn to script it for the command line. 学习为命令行编写脚本。 Or use Ant. 或者使用Ant。 You'll be glad you did. 你会很高兴的。

Yes, it will search all jar files in lib if you do it the second way. 是的,它将搜索lib中的所有jar文件,如果你以第二种方式执行它。 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 files need to be specified by name in the Classpath variable. Jar文件需要在Classpath变量中按名称指定。 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. 需要注意的一点是命令行-classpath参数比环境变量更通用,因为它允许您为每个应用程序设置类路径。

In Java 1.6+ you can set the classpath to a directory followed by /* to load all JAR files in that directory. 在Java 1.6+中,您可以将类路径设置为后跟/ *的目录,以加载该目录中的所有JAR文件。 Not just the directory name though - that's for loading class files in that directory and subdirectories. 不仅仅是目录名称 - 用于加载该目录和子目录中的类文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM