I had a similar problem attempting to install the Java 8 JDK on one of my Google Compute Engine instances and found a solution here (but read about modifications below first):
http://cgrant.io/tutorials/gcp/compute/gce/how-to-deploy-a-java-application-to-google-compute-engine/
In the "Deploy scripts" section, inside the code block shown for install.sh, I pulled out the applicable lines and ran them with a few modifications as follows:
- I ran all commands with sudo, but it would probably be more appropriate to do as he does and "su -" (change to root) before you run any of the commands.
- Changed owner and group to my own as I got permissions errors despite running all commands with sudo (you won't need to do this if you run commands as root instead of sudo-ing everything as yourself).
- Ran "apt-get install dirmngr" before running apt-key as I got an
error that the command did not exist.
- Added the --allow-unauthenticated flag to the
"apt-get install openjdk-8-jdk -y" command.
Here are the actual commands I was able to use to successfully install the Java 8 Open JDK:
# Grant yourself permission to the necessary directory
sudo chown $YOUR_USER_NAME /etc/apt/sources.list.d
sudo chgrp $YOUR_GROUP_ID /etc/apt/sources.list.d
sudo echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list
sudo echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list
sudo apt-get install dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
sudo apt-get update
sudo apt-get install openjdk-8-jdk -y --allow-unauthenticated
For the Oracle Java 8 JDK:
# Grant yourself permission to the necessary directory
sudo chown $YOUR_USER_NAME /etc/apt/sources.list.d
sudo chgrp $YOUR_GROUP_ID /etc/apt/sources.list.d
sudo echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list
sudo echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list
sudo apt-get install dirmngr
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
sudo apt-get update
sudo echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | sudo /usr/bin/debconf-set-selections
sudo apt-get install oracle-java8-installer -y --allow-unauthenticated
At this point, if all went as intended, the following commands should produce something that looks a lot like:
$ javac -version
javac 1.8.0_171
$ java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-8u171-b11-1~deb9u1-b11)
OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode)
Good luck and please let me know if this works for you. If not, I'd be happy to assist you further as needed.
Eric