This sample command line application demonstrates how to access the Google Compute Engine API using the Google Java API Client Library.
- Register your application with Google Cloud Console.
- Visit the Cloud Console
- If this is your first time then click "Create Project," otherwise you can reuse an existing project by click on it. 1. Note: You will need to enable billing for new projects by clicking on Compute Engine in the left hand navigation menu.
- Click on "APIs & auth" in the left hand navigation menu and then the "Registered Apps" subitem.
- Click on "Register App," enter "GCE Sample," as the name, choose the "Native" type and click the "Register," button.
- Expand the "OAuth 2.0 Client ID" section of the screen and download the
JSON file using the button supplied. Later on you will copy this downloaded
file to
src/main/resources/client_secrets.json
. - Click "Overview" in the left hand navigation and copy your Project ID into into
/src/main/java/com/google/api/services/samples/computeengine/cmdline/ComputeEngineSample.java
so that the following line is updated. Not performing this step will result in an 400: Bad Request error; specifically, "Invalid valueYOUR_PROJECT_ID
". For more information see setting your Project ID.
private static final String projectId = "YOUR_PROJECT_ID"`
- Code Checkout Instructions
- Pre-requisites: install Java 6, Git, and Maven.
You may need to set your
JAVA_HOME
environment variable as well. - Download code:
cd some_directory git clone https://github.com/GoogleCloudPlatform/compute-getting-started-java.git cd compute-getting-started-java cp ~/Downloads/client_secret_[id-unique-to-your-project-application].json src/main/resources/client_secrets.json nano src/main/java/com/google/api/services/samples/computeengine/cmdline/ComputeEngineSample.java
- In your editor update the
YOUR_PROJECT_ID
value as specified above. Save and exit the editor. - Compile code using Maven using the following command:
mvn compile
- Execute code using Maven via the following command:
mvn -q exec:java
- Import the code into Eclipse and running it from there
- Prerequisites: install Eclipse and the Maven plugin for Eclipse.
- Download code as specified above.
- File -> Import -> Maven -> Existing Maven Projects -> Next.
- Select your project directory as your "Root Directory," and click "Finish."
- Right-click on project compute-engine-cmdline-sample
- Run As > Java Application
- If asked, type "ComputeEngineSample" and click OK
- Application output will display in the Console.