- Java 9 Programming By Example
- Peter Verhas
- 412字
- 2021-07-02 23:37:27
Setting JAVA_HOME
The JAVA_HOME environment variable plays a special role for Java. Even though the JVM executable, java.exe or java, is on the PATH (thus you can execute it by typing the name java without specifying directory on the Command Prompt) (Terminal), it is recommended that you use the correct Java installation to set this environment variable. The value of the variable should point to the installed JDK. There are many Java-related programs, for example, Tomcat or Maven, that use this variable to locate the installed and currently used Java version. In Mac OS X, setting this variable is unavoidable.
In OS X, the program that starts to execute when you type java is a wrapper that first looks at JAVA_HOME to decide which Java version to start. If this variable is not set, then OS X will decide on its own, selecting from the available installed JDK versions. To see the available versions, you can issue the following command:
~$ /usr/libexec/java_home -V
Matching Java Virtual Machines (10):
9, x86_64: "Java SE 9-ea" /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
1.8.0_92, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home
1.7.0_60, x86_64: "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
You will then get the list of installed JDKs. Note that the command is lowercase, but the option is capital. If you do not provide any options and argument to the program, it will simply return the JDK it thinks is the newest and most appropriate for the purpose. As I copied the output of the command from my Terminal window, you can see that I have quite a few versions of Java installed on my machine.
The last line of the program response is the home directory of JDK, which is the default. You can use this to set your JAVA_HOME variable using some bash programming:
export JAVA_HOME=$(/usr/libexec/java_home)
You can place this file in your .bashrc file, which is executed each time you start Terminal application and thus JAVA_HOME will always be set. If you want to use a different version, you can use -v, with the lower case option this time, to the same utility, as follows:
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
The argument is the version of Java you want to use. Note that this versioning becomes:
export JAVA_HOME=$(/usr/libexec/java_home -v 9)
If you want to use Java JDK Early Access version and not 1.9, there is no explanation for the same—fact of life.
Note that there is another environment variable that is important for Java-CLASSPATH. We will talk about it later.
- 黑客攻防從入門到精通(實戰(zhàn)秘笈版)
- HornetQ Messaging Developer’s Guide
- C# Programming Cookbook
- Developing Mobile Web ArcGIS Applications
- 從0到1:Python數(shù)據(jù)分析
- C# 8.0核心技術(shù)指南(原書第8版)
- Learning Concurrent Programming in Scala
- Python機器學(xué)習(xí):預(yù)測分析核心算法
- 零基礎(chǔ)學(xué)C語言程序設(shè)計
- MATLAB GUI純代碼編寫從入門到實戰(zhàn)
- PHP 7從零基礎(chǔ)到項目實戰(zhàn)
- 軟件工程基礎(chǔ)與實訓(xùn)教程
- Data Science Algorithms in a Week
- Web程序設(shè)計:ASP.NET(第2版)
- 零基礎(chǔ)學(xué)C++(升級版)