Skip to content

Instantly share code, notes, and snippets.

@david-sanabria
Last active August 13, 2019 18:04
Show Gist options
  • Save david-sanabria/ab480f7c7c233f07525f19b8c99eb259 to your computer and use it in GitHub Desktop.
Save david-sanabria/ab480f7c7c233f07525f19b8c99eb259 to your computer and use it in GitHub Desktop.
A simple BASH script to download and install the latest OpenJDK v12 from jdk.java.net.
#!/bin/bash
# install-jdk12.sh
# Written: 2019-08-13
# Author: David.Sanabria@osi.ca.gov
# License: A-GPL
#
# This handy shell script downloads OpenJDK 12.02 from the OpenJDK website
# Inspired by some instructions from https://solarianprogrammer.com/2018/09/28/installing-openjdk-macos/
#
echo Check Current Java Version
java -version
# Download from OpenJDK web site
# This is a manual step: https://jdk.java.net/12/
CUR=$PWD
TMP_DIR=/tmp/openjdk
mkdir $TMP_DIR
cd $TMP_DIR
# Scrape page to find tar.gz file, which we will then download
echo "Scraping the current version from https://jdk.java.net/12/"
curl https://jdk.java.net/12/ | grep -o -e https://.*osx-x64_bin\.tar\.gz -m 1 > $TMP_DIR/jdk.txt
echo "Found $(cat $TMP_DIR/jdk.txt)"
# Check result
# Download File
echo "Downloading package ..."
curl $(cat jdk.txt) > $TMP_DIR/jdk.tar.gz
# Untar
echo "Extracting package ..."
tar xf $TMP_DIR/jdk.tar.gz
# Check
# Move to Java directory, do not replace existing
echo "Moving package to /Library/Java/JavaVirtualMachines/"
mv -nv $(ls -d $TMP_DIR/*.jdk) /Library/Java/JavaVirtualMachines/
echo "We need to use SUDO to change file ownership on the new package:"
sudo chown -R root:wheel /Library/Java/JavaVirtualMachines/jdk-*
echo "Testing Java version and function"
java -version
echo 'class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } }' >hello.java
java hello.java
echo "Cleaning Up ..."
cd $CUR
rm -Rf $TMP_DIR
echo "All Done. New Java is at:"
ls -alFd /Library/Java/JavaVirtualMachines/jdk-*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment