Setting Up a Java Development Environment

Stay organized with collections Save and categorize content based on your preferences.

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.

Install a JDK (Java Development Kit)

Install an OpenJDK 11 or OpenJDK 8 distribution. The following is a list of some of the distributions you can use:

Ensure that the following environment variables are set:

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

Install a build automation tool

Apache Maven, Gradle, and SBT are package management options that can help build Java app dependencies quickly and consistently across platforms.

Install the gcloud CLI

The gcloud CLI is a set of tools for Google Cloud. It contains gcloud, gsutil, and bq, which you can use to access Compute Engine, Cloud Storage, BigQuery, and other products and services from the command line. You can run these tools interactively or in your automated scripts.

(Optional) Install an IDE or editor

Popular editors (in no particular order) used to develop Java apps include, but are not limited to:

These editors (sometimes with the help of plugins) give you everything from syntax highlighting, intelli-sense, and code completion to fully integrated debugging capabilities.

(Optional) Install an IDE plugin

For access to helpful functions within your editor, check out the following plugins:

Install the Cloud Client Libraries for Java

Use the Cloud Client Libraries for Java to integrate with Google Cloud services, such as Datastore and Cloud Storage. You can install the package for an individual API, such as BigQuery, as shown in the following example.

If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

<!--  Using libraries-bom to manage versions.
See https://github.com/GoogleCloudPlatform/cloud-opensource-java/wiki/The-Google-Cloud-Platform-Libraries-BOM -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.11.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-bigquery</artifactId>
  </dependency>
</dependencies>

If you are using Gradle, add the following to your dependencies:

implementation platform('com.google.cloud:libraries-bom:26.11.0')

implementation 'com.google.cloud:google-cloud-bigquery'

If you are using sbt, add the following to your dependencies:

libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "2.24.3"

If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

Set up authentication

To run the client library, you must first set up authentication.

Use the client library

// Imports the Google Cloud client library
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Dataset;
import com.google.cloud.bigquery.DatasetInfo;

public class QuickstartSample {
  public static void main(String... args) throws Exception {
    // Instantiate a client. If you don't specify credentials when constructing a client, the
    // client library will look for credentials in the environment, such as the
    // GOOGLE_APPLICATION_CREDENTIALS environment variable.
    BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();

    // The name for the new dataset
    String datasetName = "my_new_dataset";

    // Prepares a new dataset
    Dataset dataset = null;
    DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();

    // Creates the dataset
    dataset = bigquery.create(datasetInfo);

    System.out.printf("Dataset %s created.%n", dataset.getDatasetId().getDataset());
  }
}

Configure endpoints for the client library

If you use APIs that support regional endpoints, use endpoints to configure which server to send requests to. For example, with the Google.Cloud.Dataproc.V1 API, you might configure a client endpoint. You can read more about regional endpoints for Dataproc here. Be sure to replace MY-PROJECT with your project name and us-central1 with the region appropriate for your configuration in the following example:

ClusterControllerSettings settings =
     ClusterControllerSettings.newBuilder()
        .setEndpoint("us-central1-dataproc.googleapis.com:443")
        .build();
 try (ClusterControllerClient clusterControllerClient = ClusterControllerClient.create(settings)) {
   String projectId = "MY-PROJECT";
   String region = "us-central1";
   Cluster cluster = Cluster.newBuilder().build();
 }

What's next

(Optional) Use the Maven or Gradle plugin for App Engine

If you are developing in the App Engine standard or flexible environment, you can use plugins for both Apache Maven and Gradle build tools that provide convenient functions for developing, testing, and deploying your apps directly.

For App Engine standard environments

Use the Maven App Engine plugin or Gradle plugin for App Engine standard environments.

For App Engine flexible environments

Use the Maven App Engine plugin or Gradle plugin for App Engine flexible environments.

More resources