Setting Up a Java Development Environment

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

Objectives

  • Install a JDK (Java Development Kit)
  • Install a build automation tool
  • (Optional) Install an IDE or editor
  • (Optional) Install IDE Google Cloud SDK Plugin
  • Install the Google Cloud SDK
  • Install the Google Cloud Client Library for Java

Install a JDK (Java Development Kit)

Install either the Java 8 SE JDK or Open JDK 8. The environment variables should be set as follows when you are done:

  • JAVA_HOME: Should point to the base of the JDK installation.
  • PATH: Should include $PATH/bin

Windows

To install the Java 8 SE JDK on Windows, open the Java downloads page and locate the most recent Java SE 8 package section.

Java 8 Downloads

Agree to the terms and conditions, download and run the appropriate installer package.

Mac OS X

To install the Java 8 SE JDK on Mac OS, download and run the Mac installer.

Linux

Java JDK 8

If the Java 8 SE JDK is not available on your Linux distribution, you can open the Java downloads page and locate the most recent Java SE 8 package section.

Java 8 Downloads

Agree to the terms and conditions, download and run the appropriate installer package.

Open JDK

Open JDK is an open source implementation of the Java Platform SE and related projects. You can download and install Open JDK with the following commands:

Debian, Ubuntu, etc.

sudo apt-get install openjdk-8-jdk

Fedora, Oracle Linux, Red Hat Enterprise Linux, etc.

su -c "yum install java-1.8.0-openjdk-devel"

Install a build automation tool

Apache Maven and Gradle are package management options which can help build java app dependencies quickly and consistently across platforms.

Install the Google Cloud SDK

The Google Cloud SDK is a set of tools for Google Cloud Platform. It contains gcloud, gsutil, and bq, which you can use to access Google Compute Engine, Google Cloud Storage, Google 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 applications 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, maximizing your Java development efficacy.

(Optional) Install IDE Google Cloud SDK Plugin

For access to helpful functions within your editor, check out the Google-sponsored plugins for Eclipse or IntelliJ.

Install the Google Cloud Client Library for Java

The Google Cloud Client Library for Java is the idiomatic way for Java developers to integrate with Google Cloud Platform services, like Cloud Datastore and Cloud Storage. You can install the package for an individual API, such as BigQuery, as shown in the following example.

Installing the client library

If you are using Maven, add this to your pom.xml file:
<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-bigquery</artifactId>
  <version>1.28.0</version>
</dependency>
If you are using Gradle, add this to your dependencies:
compile 'com.google.cloud:google-cloud-bigquery:1.28.0'
If you are using SBT, add this to your dependences:
libraryDependencies += "com.google.cloud" % "google-cloud-bigquery" % "1.28.0"

Set up authentication

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

Using 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());
  }
}

Next Steps

(Optional) Install 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

Add the Maven App Engine Plugin or Gradle Plugin for App Engine Standard environments.

For App Engine Flexible Environments

Add the Maven App Engine Plugin or Gradle Plugin for App Engine Flexibe environments.

More Resources

Send feedback about...