I want to install Java entirely from the terminal without using any GUI. Can anybody give some instructions? I know that I download it with curl, but it seems that curl doesn't play quite nicely with Oracle, and I have no idea what to do after downloading it.

Edit: I want to use Oracle's JDK

up vote 5 down vote accepted

To entirely use a (bash) shell to install e.g. Java 8u112 JRE do the following:

Download:

cd ~/Downloads
curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jre-8u112-macosx-x64.dmg > jre-8u112-macosx-x64.dmg

Mount:

hdiutil attach jre-8u112-macosx-x64.dmg

Install:

sudo installer -pkg /Volumes/Java\ 8\ Update\ 112/Java\ 8\ Update\ 112.app/Contents/Resources/JavaAppletPlugin.pkg -target /

Clean-up:

diskutil umount /Volumes/Java\ 8\ Update\ 112 
rm jre-8u112-macosx-x64.dmg

To install the latest Oracle JDK (at the time of writing this answer it's Java 8u121 JDK) the various paths (besides the version number) are slightly different compared to the older JRE version. The URL contains an additional directory (i.e. /e9e7ea248e2c4826b92b3f075a80e441) and the dmg file contains a .pkg file instead of an installer app:

cd ~/Downloads
curl -v -j -k -L -H "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-macosx-x64.dmg > jdk-8u121-macosx-x64.dmg
hdiutil attach jdk-8u121-macosx-x64.dmg
sudo installer -pkg /Volumes/JDK\ 8\ Update\ 121/JDK\ 8\ Update\ 121.pkg -target /
diskutil umount /Volumes/JDK\ 8\ Update\ 121
rm jdk-8u121-macosx-x64.dmg
  • This successfully installed Java, thanks. The weird thing is it says unmount failed and the disk is not unmounted, meaning that there is something wrong with diskutil umount /Volumes/Java\ 8\ Update\ 121. Can you fix this? – paper1111 Mar 19 '17 at 12:03
  • @paper1111 Fixed! The volume name is "JDK..." instead of "Java..." – klanomath Mar 19 '17 at 16:54
  • I was trying to install from the command line but could not find a pkg installer online any more. I saw that JavaAppletPlugin.pkg in the DMG but thought it was not what I wanted because the name is misleading. I tried using installer the way you did but it hung just like the GUI version. I forced quit it and used the options I had used before with pkg installers and this worked: installer -verboseR -dumplog -pkg "/Volumes/Java 8 Update 131/Java 8 Update 131.app/Contents/Resources/JavaAppletPlugin.pkg" -target / -store The -store emulates installation from the Mac App Store. – hmj6jmh Jul 5 '17 at 21:44
  • @hmj6jmh The question only related to the Java SDK! You are trying to install the runtime... – klanomath Jul 5 '17 at 21:45

The best/safest way to install Java on MacOS is with Homebrew; just this:

brew cask install java

Or if you don’t have Homebrew installed already, just one additional command first:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew cask install java

protected by bmike Oct 8 '17 at 13:09

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.