简体   繁体   中英

Why I do not need to set CLASSPATH and JAVA_HOME in Mac OS X?

I am using jdk 1.7, after Installation I did not set JAVA_HOME or CLASSPATH environment variable, and I use it to compile a servlet class and everything worked fine.

So I want to known, why I am not supposed to set these Environment variables ? Can JDK auto resolve CLASSPATH ?

JAVA_HOME is used by some tools (Ant, Maven, etc.), but is not needed by Java (or javac) itself.

CLASSPATH should never be used. It sets a global classpath that is supposed to be common to several Java programs installed on your computer, which is completely unrealistic. Every program should have its own classpath passed as argument to the command using the -classpath option.

And that's true on all the platforms.

Most of the Unixes and Mac use the so-called Bash Shell in the "Terminal". When you launch an executable program (with file permission of executable) in a Bash shell, the system searches the program in ALL the directories listed in the PATH.

The have JAVA_HOME path and can be set as:

setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Home

To setup JAVA_HOME :

$ vi ~/.bash_profile 

export JAVA_HOME=$(/usr/libexec/java_home)

$ source ~/.bash_profile

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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