14

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

7

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
| improve this answer | |
  • 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
22

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

brew cask install java

Or if you don’t have Homebrew installed already, just type this command first:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .