Skip to content

Instantly share code, notes, and snippets.

@schafeld
Last active January 25, 2022 00:02
Show Gist options
  • Save schafeld/e81986798dbc23028d72d49c8159dd52 to your computer and use it in GitHub Desktop.
Save schafeld/e81986798dbc23028d72d49c8159dd52 to your computer and use it in GitHub Desktop.
Setting Up a Java 8 Development Environment in Google Cloud Platform (Debian, VM)

GCP Qwiklabs – Setting Up an (outdated) Java Development Environment on GCP

Problem: The course instructions use an outdated Java 8 version. The simple server for the quiz would not compile in Java 11.

It was tricky to set up an outdated Java version. So here are updated instructions to pass the checkpoints (this is well within the Coursera student honor code as it is just an update of the official instructions).

Part of a Coursera MOOC on Google Cloud Platform

Qwiklabs – Setting Up a Development Environment: Java

In the SSH session, to update the Debian package list, enter the following command:

sudo apt-get update

Install Git:

sudo apt-get install git -y

Java 8 DEPRECATED / something about Oracle changing licensing or something.

Workaround found here: https://stackoverflow.com/questions/57031649/how-to-install-openjdk-8-jdk-on-debian-10-buster https://adoptopenjdk.net/installation.html?variant=openjdk8&jvmVariant=openj9

Maybe search for available wget versions:

apt-cache search wget

If you have more than one Java versions installed on your system. Run this command to set the default Java (I didn't need it): sudo update-alternatives --config java

Install wget for GCP's Debian VM:

sudo apt-get install wget2

Import the official AdoptOpenJDK GPG key by running the following command (install Java 8 workaround):

wget2 -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -

Make sure the repository will be found:

sudo apt-get install -y software-properties-common

Import the AdoptOpenJDK DEB repository by running the following command:

sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/

Refresh package list with sudo apt-get update and then install AdoptOpenJDK package:

sudo apt-get update && sudo apt-get install adoptopenjdk-8-hotspot

Check for Java version:

java -version

Output should be:

   openjdk version "1.8.0_252"
   OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_252-b09)
   OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.252-b09, mixed mode)

In case there are several Java versions installed and you want to switch (select from the presented versions or confirm default):

sudo update-alternatives --config java

Install Maven:

sudo apt-get install -yq maven

Configure IP tables:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

Export the Project ID as an environment variable:

export GCLOUD_PROJECT="$(curl -H Metadata-Flavor:Google http://metadata/computeMetadata/v1/project/project-id)"

To check the version of Java, enter the following command:

java -version

Clone the class repository:

git clone https://github.com/GoogleCloudPlatform/training-data-analyst

To keep the navigation simple, create a soft link as a shortcut to the working directory:

ln -s ~/training-data-analyst/courses/developingapps/v1.2/java/devenv ~/devenv

Change to the directory that contains the sample files:

cd ~/devenv

Run a simple web application:

mvn clean install

Run the application:

mvn spring-boot:run

Return to the Cloud Console VM instances list, and click on the External IP address for the dev-instance.

TADA! 🤓🎉

To run a simple Java application that lists Compute Engine instances, execute the following command:

mvn exec:java@list-gce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment