How do I set the default Java (JDK) version on Mac OS X?

In this post you will learn how to set the default JAVA_HOME in OS X when you have more than one JDK installed in your computer. First you need to run /usr/libexec/java_home -V command to get the list of installed JDK. The command will print out something like the following depending on the available JDK in your computer.

On my machine I have the following version of Java.

Matching Java Virtual Machines (3):
    9, x86_64:  "Java SE 9"     /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
    1.8.0_121, x86_64:  "Java SE 8"     /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home
    1.7.0_80, x86_64:   "Java SE 7"     /Library/Java/JavaVirtualMachines/jdk1.7.0_80.jdk/Contents/Home

From the list above pick which version you want to be the default JDK. For example I will choose the 1.8.0_121 version to be my default JDK. To set it run the command below.

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_121`

If the major version of the available JDK is unique you can just use the major version, like:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

After setting the JAVA_HOME and you run the java -version command you will see that JDK 1.8 is the new default JDK in your computer.

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

The change above will only active in the current running shell. If you close or terminate the shell, next time you open the shell you will need to set it again. To make this change permanent you need to set it in your shell init file. For example if you are using bash then you can set the command in the .bash_profile. Add the following lines at the end of the file.

# Setting default JDK to version 1.8.
export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

To activate this configuration right away your can run source .bash_profile. This command reads and executes the .bash_profile in the current shell.

How do I run FTP server in Mac OS X?

I need to test FTP client codes, so I need to find an FTP server for testing my codes. After searching for a while I find out that OS X already equipped FTP server. I am currently using OS X El Capitan 10.11.*.

Let’s now test the FTP server on Mac OS X with the following steps:

  • Launch the Terminal.app
  • Type the following command to start the FTP server.
sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist
  • Connect to the FTP server by running ftp localhost command.
  • We’ll be asked to enter the username and password.
$ ftp localhost
Trying ::1...
Connected to localhost.
220 ::1 FTP server (tnftpd 20100324+GSSAPI) ready.
Name (localhost:wsaryada): wsaryada
331 User wsaryada accepted, provide password.
Password: 
230 User wsaryada logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>
  • If we see the messages above and the ftp> prompt means that the FTP server works and ready to accept our command.
  • We can also try to access the FTP server using a browser. In the URL box type ftp://localhost to connect. We need to supply username and password to login.
  • To exit or close the connection to FTP server we can run the exit command.
  • Finally, to shutdown the FTP server we run:
sudo -s launchctl unload -w /System/Library/LaunchDaemons/ftp.plist

After the FTP server ready, I can now continue to create some test program to access the FTP server. There are already some examples you can find in the Apache Commons Net category that use the FTPClient library to access FTP server.