'How to resolve class file for com.google.cloud.Service not found

I am Trying to upload JSON data to gcs. As I did not use google cloud previously I started with uploading random String to gcs but I got stuck at the beginning itself while creating a Storage service object

Maven dependency

 <dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
   <version>1.70.0</version>
 </dependency>


import com.google.cloud.storage.*;
Storage storage = StorageOptions.getDefaultInstance().getService();
    BlobId blobId = BlobId.of("bucket_name", "test_upload/test.txt");
    BlobInfo blobInfo = BlobInfo.newBuilder(blobId).setContentType("text/plain").build();
    Blob blob = storage.create(blobInfo, "Hello, Cloud Storage!".getBytes(UTF_8));
    System.out.println(blob);

Compile Time Error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project gcshelper: Compilation failure: Compilation failure: 
ERROR] /Users/v3/gcshelper/src/main/java/com/tv/gcs/GcsTest.java:[16,41] cannot access com.google.cloud.ServiceOptions [ERROR] class file for com.google.cloud.ServiceOptions not found [ERROR] /Users/v3/gcshelper/src/main/java/com/tv/gcs/GcsTest.java:[19,28] cannot access com.google.cloud.Service [ERROR] class file for com.google.cloud.Service not found [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging.


Solution 1:[1]

<dependency>
 <groupId>com.google.cloud</groupId>
 <artifactId>google-cloud</artifactId>
 <version>0.47.0-alpha</version>
</dependency>

solved my issue

Solution 2:[2]

If someone is using module-info.java then you need to have these two dependencies:

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-core</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-firestore</artifactId>
        <version>3.0.21</version>
    </dependency>

and in your module-info.java do something like:

open module ModuleName {
    ...
    requires google.cloud.core;
    requires google.cloud.firestore;
    requires com.google.auth;
    requires com.google.auth.oauth2;
    ...
}

if you are using intellij, then it will ask you to do google.cloud.firestore and com.google.auth.oauth2 but this won't work as these depend on google.cloud.core and com.google.auth.

Solution 3:[3]

Did you try to include this dependency in your pom.xml ?

<dependency>
    <groupId>com.google.cloud</groupId>
    <artifactId>google-cloud-core</artifactId>
    <version>1.70.0</version>
</dependency> 

Solution 4:[4]

my understanding is that you can’t upload files to the gcp container using java, and the stack trace is showing that maven failing at compilation time.

Well, You can try 2 things:

1.- Make sure that you Authentication settings are good, You can follow the steps in the link[1]. 2.- Configure you Maven installation according the gcp instructions[2].

[1]https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-java [2]https://cloud.google.com/appengine/docs/standard/java/tools/maven#setting_up_maven

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kiran kumar
Solution 2 MK Al
Solution 3 hkanjih
Solution 4 Enrique Del Valle