Home » Java » How to set GOOGLE_APPLICATION_CREDENTIALS in spring boot app

How to set GOOGLE_APPLICATION_CREDENTIALS in spring boot app

Posted by: admin December 28, 2021 Leave a comment


I am attempting to use the google vision library in java. The steps specify that I need to setup my auth credentials in order to start using the this library . I was able to generate my json property file from API Console Credentials page and I placed it in my spring boot app in the resources folder.

I think updated my application.properties file to include the value like so:


I’m also setting my property source in my controller like so:


However, after doing that I’m still getting an error saying:

java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

I was able to configure this property using Spring Cloud GCP spring-cloud-gcp-starter-data-datastore, creating a service account project owner, copy the JSON private key to the main resources directory, and setting the following properties in the application.properties


from the documentation

You can find the project id by visiting this page https://support.google.com/googleapi/answer/7014113?hl=en
Go to the API Console.

From the projects list, select Manage all projects. The names and IDs for all the projects you’re a member of are displayed.
You can also select the project go the settings and see the project ID


You need to set the shell variable. Run this command before mvn run.

export GOOGLE_APPLICATION_CREDENTIALS="/Users/ronnyshibley/Dev/eddress-service-key.json"


You can use application properties, but you need to use a different StorageOptions builder.

You are probably using

private static Storage storage = StorageOptions.getDefaultInstance().getService(); 

But if you want to skip the environment variable you need to use:

Credentials credentials = GoogleCredentials
  .fromStream(new FileInputStream("path/to/file"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials)

Note that the default builder (using environment variables) is better if you are going to deploy your applications to cloud, because then this is automatically filled for you.


For authentication using the service account key, you can set the Environment Variable in your shell.

export GOOGLE_APPLICATION_CREDENTIALS="/Users/username/directory/service-key-file-name.json"

Then you need to start your IDE from the same session. I was stuck after exporting and setting up the environment variable and was still unable to use it.

I tried quitting the current IDE window and restarted the IDE again from the same session.
In my case it was Intellij, so in the terminal itself,

cd project directory
idea .

Or you can also add the environment variable in your bash profile and then source it.