How to install Java on your mac
Originally Sun Micro Systems developed Java. Then it was purchased by Oracle who is also know for having created the Oracle Database. Oracle creates the official Oracle versions of Java.
There is also an open source version of Java called OpenJDK.
Here we will stick with the Oracle version of Java.
If you want to develop with Java you need something called the JDK. If you only want to run Java you will need the JRE (Java Runtime Environment).
The JRE is also contained in the JDK.
Installing Software on the mac is usually done with Homebrew. However with Oracle versions of Java it is not supported properly - there are some issues with licenses.
Homebrew is good for installing OpenJDK versions of java.
In this example we want to install the Oracle JDK so read on.
The Java Runtime Environment runs platform independent byte code. There are different JRE's for different operating systems. The JRE interprets platform independent code and makes native calls to the C code libraries of the native operating system. In fact when you get into Java more you will be able to drill into the source code of the JDK and see that there are JNI (Java Native Interface) calls which make the calls to the methods / functions of the underlying operating system.
You wrote your code as text. Typically the IDE (Integrated Development Environment) like Eclipse, STS or IntelliJ compiles the text source code into platform independent Byte Code. The same platform independent byte code can be run on different operating systems as long as they have the Java Runtime Environment installed.
The command for compiling is:
javac
Note there are a bunch of arguments and flags that can be used for compiling.
The command for running is:
java
Note there are a bunch of arguments and flags that can be used for running a java application.
Oracke JDK dmg packages can be downloaded from
https://www.oracle.com/ke/java/technologies/downloads/
Choose the MacOS tab to see which version of Java you can download for MacOS operating system.
If you have an up to date Mac choose the x64 DMG installer.
Download and double click the DMG and install it as you would any other DMG package.
Note over time if you come back to the download page you will see a new version of Java. You do NOT need to uninstall the old version before installing the newer version.
In fact different applications may need different versions of Java installed on your machine.
As you may have different versions of JDK installed, let us explain how you can set up your mac so you can switch versions of JDK easily.
There is the concept of the PATH variable, When you type something like:
java
or
javac
on the command line. The executables need to be in the path. Otherwise you will get errors like:
zsh: command not found: java
So this is where the concept of PATH comes in. the PATH variable needs to specify the location of the directory where the executable resides.
When you type something like java on the command line it will look through the directories specified by the PATH variable and find the first java it finds in the PATH and execute that.
Certain directories on your Mac are already added to the PATH variable. However certain directories are not and you need a way of updating the PATH variable.
When you get an error like:
zsh: command not found: some_command
it means either you did not install the command or the directory where that command executable lives is not in the PATH.
To see what the current value of the PATH variable is open a terminal and type:
johndickerson@Johns-MacBook-Pro ~ % echo $PATH
/Users/johndickerson/.pyenv/shims:/Users/johndickerson/.nvm/versions/node/v18.9.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Applications/Docker.app/Contents/Resources/bin
Note you can see there is a whole bunch of directories separated with the : seperator.
You could manually change the value of PATH as follows:
export PATH=/somedirectory:$PATH
The above would have added /somedirectory to the beginning of the PATH. Note that the operating system execute the first orccurence of a command it finds in the path.
When you mac starts up certain directories are added to the PATH variable. There are certain scripts which call other scripts.
When you open a terminal window to get the command line certain scripts are called.
If we want to edit the PATH variable we need to find which shell script file gets called on opening a terminal window.
This is quite easy to find out. Open a terminal window and type:
cd ~
ls -a | grep z
~ is a unix symbol which means your home directory
ls is a command for showing what is in a directory
The -a flag of the ls command means also show hidden files. On unix systems like MacOS hidden files start with a dot.
| is a pipe and is piping the output of the ls command into the grep command.
grep is used for finding text.
When your run the command above you will see something like:
johndickerson@Johns-MacBook-Pro ~ % ls -a | grep z
.zprofile
.zsh_history
.zshrc
Note the above command showed us all files which have z in their name. In this example we see we have 3 files with z in the name.
Basically the MacOS calls .zprofile and then later calls .zshrc if it does not exist.
Probably you do not have a .zshrc file yet. If you do not have one you need to CREATE one.
To create and edit files it is good to know the vi or vim command line editor.
https://www.redhat.com/sysadmin/introduction-vi-editor
https://www.youtube.com/watch?v=S24LN5h_pac
Later you could be SSH to a remote server on the cloud and you need to be able to edit files on the command line. vi is an editor which is always available on remote servers so make sure you learn how to use it.
You will be adding content similar to the following in it:
# created by JD - is called by OS after .zprofile
echo "Hello JD"
export JAVA_16_HOME=$(/usr/libexec/java_home -v16.0.1)
export JAVA_14_HOME=/Users/johndickerson/Applications/jdk-14.0.1.jdk/Contents/Home
alias java16='export JAVA_HOME=$JAVA_16_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
java16
echo "JAVA_HOME=$JAVA_HOME"
echo "==========="
echo "Aliases:"
alias
echo "==========="
Before copying the above into your .zshrc you need to understand it first.
Note that we could have added something like:
export PATH=/Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home:$PATH
This would have added the directory at the front of the PATH.
However it is not that easy finding the PATH of your java. To find it you could have typed:
johndickerson@Johns-MacBook-Pro ~ % whereis java
/usr/bin/java
johndickerson@Johns-MacBook-Pro ~ % ls -ltr /usr/bin/java
lrwxr-xr-x 1 root wheel 74 Oct 29 2019 /usr/bin/java -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
Note that we have "lrwxr-xr-x" - that means we have a symn link and that /usr/bin/java is mapped to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
However we cannot see what the current version is.
This is why we are making use of:
/usr/libexec/java_home
This tool helps us. To see what versions of java you have installed already (using DMG files):
johndickerson@Johns-MacBook-Pro ~ % /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
16.0.1, x86_64: "Java SE 16.0.1" /Library/Java/JavaVirtualMachines/jdk-16.0.1.jdk/Contents/Home
1.8.0_311, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home
You can see that we have 2 versions of JDK installed: 16.0.1 and 8
We can set the directory location of the version of our choice using the java_home executable:
export JAVA_16_HOME=$(/usr/libexec/java_home -v16.0.1)
In this example we want to use java version v16.0.1
So let us revisit the code I put in the .zshrc:
export JAVA_16_HOME=$(/usr/libexec/java_home -v16.0.1)
export JAVA_14_HOME=/Users/johndickerson/Applications/jdk-14.0.1.jdk/Contents/Home
alias java16='export JAVA_HOME=$JAVA_16_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
java16
Notice that jdk-14.0.1.jdk was not shown using the java_home executable. That is because I had downloaded the jdk as a zip file and installed it in my own location which the java_home command does not know about.
I then created an alias for exporting the JAVA_HOME variable:
alias java16='export JAVA_HOME=$JAVA_16_HOME'
alias java14='export JAVA_HOME=$JAVA_14_HOME'
Finallyin the .zrch script I ran the java16 alias:
java16
To see what aliases you have configured type:
johndickerson@Johns-MacBook-Pro ~ % alias
graph='git log --all --decorate --oneline --graph'
java14='export JAVA_HOME=$JAVA_14_HOME'
java16='export JAVA_HOME=$JAVA_16_HOME'
run-help=man
which-command=whence
So once I have a terminal open I can switch to using jdk-14.0.1.jdk just by typing:
java14
Note that if you were adding the JAVA_HOME variable to the path yourself you would add the following to the shell script:
export PATH=$JAVA_HOME:$PATH
However either the java_home executable or something else did that for you.
Now that you hopefully understand what is going on edit your .zrch script.
Back: Core Basic Java
Page Author: JD