Set up a Java development environment

This tutorial shows how to prepare your local machine for Java development, including developing Java apps that run on Google Cloud. Follow these steps to install Java and relevant tools.

Objectives

  • Install a JDK (Java Development Kit).
  • Install a build automation tool.
  • Install the gcloud CLI.
  • (Optional) Install an IDE or editor.
  • (Optional) Install IDE Google Cloud plugin.
  • Install the Cloud Client Libraries for Java.
  • Set up authentication.

Install a JDK (Java Development Kit)

You may choose any Java distribution of your choice by ensuring that the following environment variables are set:

  • JAVA_HOME: Points to the base of the JDK installation.
  • PATH: Includes $JAVA_HOME/bin.

Eclipse Temurin is the recommended OpenJDK (Java Development Kit) distribution for use with Google Cloud. Temurin is open-source licensed, Java SE TCK-certified, and tested to ensure production-quality performance and security.

Temurin's installation instructions vary by operating system.

If you are using Compute Engine boot images, you can use the following installation scripts.

CentOS/RHEL/Rocky

  1. Determine the major version of CentOS/RHEL/Rocky Linux:
    eval "$(grep VERSION_ID /etc/os-release)"
    eval "$(grep ^ID= /etc/os-release)"
    OLD_IFS=$IFS
    IFS='.'
    read -ra split_version <<< "$VERSION_ID"
    IFS=$OLD_IFS
    MAJOR_VERSION=$split_version
  2. Create the Adoptium source repo file, `/etc/yum.repos.d/adoptium.repo`:
    sudo tee /etc/yum.repos.d/adoptium.repo << EOM
    [Adoptium]
    name=Adoptium
    baseurl=https://packages.adoptium.net/artifactory/rpm/$ID/$MAJOR_VERSION/\$basearch
    enabled=1
    gpgcheck=1
    gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public
    EOM
  3. Update package lists:
    sudo yum update -y
  4. Install Temurin:
    sudo yum install -y temurin-17-jdk
  5. Verify installation:
    java -version

Debian/Ubuntu

  1. Install the public repo GPG key. If you are using Ubuntu 16.4, pass the key through gpg --dearmor before saving to file. (ex: sudo wget ... | gpg --dearmor | sudo tee ...)
    sudo mkdir -p /etc/apt/keyrings
    sudo wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public |
      sudo tee /etc/apt/keyrings/adoptium.asc
  2. Determine the name of the Linux distribution, and create the source list file, /etc/apt/sources.list.d/adoptium.list:
    eval "$(grep VERSION_CODENAME /etc/os-release)"
    sudo tee /etc/apt/sources.list.d/adoptium.list << EOM
    deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $VERSION_CODENAME main
    EOM
  3. Update package lists:
    sudo apt update -y
  4. Install Temurin: