How should I set CLASSPATH?

天涯浪子 提交于 2019-12-22 05:05:26

问题


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?


回答1:


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"




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/2017833/how-should-i-set-classpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!