0

I am creating a Java Project in Eclipse that uses a Google API (Natural Language Processing), but I am getting the error:

The Application Default Credentials are not available. They are available if running on Google App Engine, Google Compute Engine, or Google Cloud Shell. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials.

Under my system settings, I have gone into System and created an environment variable (both user and system) with name: GOOGLE_APPLICATION_CREDENTIALS and a value that points to my service account JSON Key, like how it says in the documentation. I see some people in other questions also say to use a command like: export GOOGLE_APPLICATION_CREDENTIALS="path/to/json/file", but when I did that in Windows Bash it didn't do anything.

I have not added a variable or path actually in my JAVA code because I don't know how I would do that, and I couldn't find an answer in any Google docs. Below is the code that tries to use the default application credentials:

Some places say to use Google App Engine and use the default credentials, but I would rather just not upload the project to Google App Engine and use the JSON credential file I already have, seems like that would be easier.

/**
   * Connects to the Natural Language API using Application Default Credentials.
   */
  public static CloudNaturalLanguageAPI getLanguageService() 
    throws IOException, GeneralSecurityException {
    GoogleCredential credential =
        GoogleCredential.getApplicationDefault().createScoped(CloudNaturalLanguageAPIScopes.all());
    JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    return new CloudNaturalLanguageAPI.Builder(
        GoogleNetHttpTransport.newTrustedTransport(),
        jsonFactory, new HttpRequestInitializer() {
              @Override
              public void initialize(HttpRequest request) throws IOException {
                credential.initialize(request);
              }
            })
        .setApplicationName(APPLICATION_NAME)
        .build();
  }
4
  • Bash support in Windows is relatively new, it's possible Google's APIs don't integrate with it properly yet (or there's a bug in Window's Bash implementation). I ran into a similar issue with GCS on Cygwin a little while back, the workaround might work for you too.
    – dimo414
    Nov 3, 2016 at 6:50
  • Thanks for the suggestion @dimo414, I have never used the command line when creating a Java application. My understanding was that I had to link the GOOGLE_APPLICATION_CREDENTIALS to a file somewhere in my Java code, but from the workaround: $ CLOUDSDK_CONFIG=$(cygpath -w ~/.config/gcloud) \ GOOGLE_APPLICATION_CREDENTIALS=$(cygpath -w ~/.config/gcloud)/application_default_credentials.json \ java -cp GCloudDemo.jar com.google.gcloud.examples.storage.StorageExample list Bucket{name=...} it looks like it uses the command line. Could you please elaborate how I would implement that?
    – user5109957
    Nov 3, 2016 at 17:21
  • @dimo414 also, I still haven't set up Google App Engine, gcloud-java or anything like that, I just enabled my API and got my service account JSON Key file. Will I have to do anything like that before proceeding?
    – user5109957
    Nov 3, 2016 at 17:24
  • sorry, I saw you mentioned Windows Bash which made me think of my similar situation with Cygwin. You ought to be able to run the same style command from Windows Bash invoking whatever java application you'd like (including one built by Eclipse), or you can set them as environment variables as you describe in your answer.
    – dimo414
    Nov 3, 2016 at 19:43

1 Answer 1

0

It turns out that when using Eclipse, setting the GOOGLE_APPLICATION_CREDENTIALS environment variable must be done within Eclipse itself, not in the Windows System settings. All I had to do was go into Eclipse, click the Run dropdown list, select Run Configurations, open Environment tab, and set that environment variable there. I then got an error that said I had to enable billing and simply did so in my Google Cloud Platform settings. Everything worked perfectly after that!

1
  • You could still set those environment variables in Windows (if you want them to be system-wide); you just need to restart Eclipse in order for Eclipse to pick them up. If you want them to be Project or Run specific then setting them in Eclipse will work.
    – dimo414
    Nov 3, 2016 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.