title: GCP Pub/Sub throws "The Application Default Credentials are not available"
body: <p>I am trying to publish to Google Pub/Sub topic using the following:</p>
  
  <pre><code>ProjectTopicName topicName = ProjectTopicName.of("my-project-id", "my-topic-id");
  Publisher publisher = null;
  
  try {
    // Create a publisher instance with default settings bound to the topic
    publisher = Publisher.newBuilder(topicName).build();
  
    List&lt;String&gt; messages = Arrays.asList("first message", "second message");
  
    for (final String message : messages) {
      ByteString data = ByteString.copyFromUtf8(message);
      PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
  
      // Once published, returns a server-assigned message id (unique within the topic)
      ApiFuture&lt;String&gt; future = publisher.publish(pubsubMessage);
  
      // Add an asynchronous callback to handle success / failure
      ApiFutures.addCallback(
          future,
          new ApiFutureCallback&lt;String&gt;() {
  
            @Override
            public void onFailure(Throwable throwable) {
              if (throwable instanceof ApiException) {
                ApiException apiException = ((ApiException) throwable);
                // details on the API exception
                System.out.println(apiException.getStatusCode().getCode());
                System.out.println(apiException.isRetryable());
              }
              System.out.println("Error publishing message : " + message);
            }
  
            @Override
            public void onSuccess(String messageId) {
              // Once published, returns server-assigned message ids (unique within the topic)
              System.out.println(messageId);
            }
          },
          MoreExecutors.directExecutor());
    }
  } finally {
    if (publisher != null) {
      // When finished with the publisher, shutdown to free up resources.
      publisher.shutdown();
      publisher.awaitTermination(1, TimeUnit.MINUTES);
    }
  }
  </code></pre>
  
  <p>I have changed the default values you see here to the particulars of the account I am hitting. </p>
  
  <p>The environment variable points to the JSON file containing the pub/sub authentication credentials:</p>
  
  <pre><code>GOOGLE_APPLICATION_CREDENTIALS
  </code></pre>
  
  <p>was set using:</p>
  
  <pre><code>export GOOGLE_APPLICATION_CREDENTIALS=path/to/file.json
  </code></pre>
  
  <p>and verified with <code>echo $GOOGLE_APPLICATION_CREDENTIALS</code> - after a reboot.  </p>
  
  <p>But I am still encountering: </p>
  
  <pre><code>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.
  </code></pre>
  
  <p>I believe this is related to the default environment that the application is running in, or rather what GCP object thinks the context is -<code>runningOnComputeEngine</code>:</p>
  
  <pre><code>com.google.auth.oauth2.ComputeEngineCredentials runningOnComputeEngine
  INFO: Failed to detect whether we are running on Google Compute Engine.
  </code></pre>
  
  <p>also, a dialog displayed:</p>
  
  <pre><code>Unable to launch App Engine Server
  Cannot determine server execution context
  </code></pre>
  
  <p>and there are no Google Cloud Platform settings in project (Eclipse 2019-3):</p>
  
  <p><a href="https://i.stack.imgur.com/ZwuG2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZwuG2.png" alt="GCP Eclipse Settings"></a></p>
  
  <p>This is not an App Engine application.</p>
  
  <blockquote>
    <p>How to set the environment that GCP objects point to -> Non App Engine. </p>
  </blockquote>
  
  <p><strong>For reference:</strong></p>
  
  <ul>
  <li><a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application" rel="nofollow noreferrer">Server to Server</a>  (link in error message)</li>
  <li><a href="https://cloud.google.com/pubsub/docs/publisher" rel="nofollow noreferrer">Publish</a></li>
  <li><p><a href="https://cloud.google.com/eclipse/docs/quickstart" rel="nofollow noreferrer">Google Cloud Tools for Eclipse</a></p></li>
  <li><p>Java 7 application</p></li>
  <li>Mac OS (Sierra)</li>
  <li>The file permissions are set that app can read the file.</li>
  </ul>