Eugene Huang
Eugene Huang

Reputation: 63

Maven / Firebase - Cannot find symbol: variable Firestore Client

I am trying to connect to Firebase within my Java project, managed with Maven. I am following Firebase's Getting Started with Cloud Firestore exactly to set up my development environment and initialize Cloud Firestore. This is what my pom.xml and myClass.java looks like.

My dependency in pom.xml:

   <dependencies>
        <!-- This dependency is added for Firebase usage -->
        <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-admin</artifactId>
            <version>7.1.0</version>
        </dependency>

My myClass.java:

package ca.uhn.fhir.example;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.interceptor.ResponseHighlighterInterceptor;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.firestore.Firestore;

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import java.io.*;


@WebServlet("/*")
public class Example02_SimpleRestfulServer extends RestfulServer {

    @Override
    protected void initialize() throws ServletException {
        // Create a context for the appropriate version
        setFhirContext(FhirContext.forR4());
        
        // Register resource providers
        // Calls the constructor on the resource provider class? 
        registerProvider(new Example01_PatientResourceProvider());
        
        // Format the responses in nice HTML
        registerInterceptor(new ResponseHighlighterInterceptor());
    
        try {   
        // Initialize Firebase
        // Use a service account
        String credentials_path = "/my/path/dfdfdfdfd93.json";
        InputStream serviceAccount = new FileInputStream(credentials_path);
        GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);
        FirebaseOptions options = new FirebaseOptions.Builder()
            .setCredentials(credentials)
            .build();
        FirebaseApp.initializeApp(options);
        
        Firestore db = FirestoreClient.getFirestore();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

However, I run into the error: cannot find symbol [ERROR] symbol: variable FirestoreClient. I have tried adding other related dependencies as shown below, but they are not found/don't seem to fix the problem. I think I am following the Firebase tutorial exactly, so what might I be doing wrong? Why is the FirestoreClient not recognized but many of the other variables are?

        <!-- Attempting to fix errors (this doesn't seem to do anything) -->
        <!-- https://mvnrepository.com/artifact/com.google.cloud/google-cloud-firestore -->
        <!-- <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-firestore</artifactId>
            <version>2.2.4</version>
        </dependency> -->

        <!-- Not found -->
        <!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-firestore -->
        <!-- <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-firestore</artifactId>
            <version>18.1.0</version>
        </dependency> -->

        <!--Not found-->
        <!-- https://mvnrepository.com/artifact/com.google.firebase/firebase-core -->
        <!-- <dependency>
            <groupId>com.google.firebase</groupId>
            <artifactId>firebase-core</artifactId>
            <version>18.0.2</version>
        </dependency> -->

Upvotes: 0

Views: 802

Answers (1)

Rafael Lemos
Rafael Lemos

Reputation: 5829

This is happening because you did not import the FirestoreClient class, so add the following import to your Example02_SimpleRestfulServer class and it will be fixed:

import com.google.firebase.cloud.FirestoreClient;

NOTE: This should also be in the example in the documentation example you shared, I would recommend you to open a Bug Report in Google's IssueTracker for they to fix that documentation, if you'd like.

Upvotes: 1

Related Questions