-1

I tried FileNotFoundException and try, catch but did not help. I think problem is here

InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");

Full class source code:

package uz.xose.webapp;

import java.io.FileInputStream;
import java.io.InputStream;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.cloud.FirestoreClient;



@RestController
public class HelloController { 

    InputStream serviceAccount = new FileInputStream("/src/main/resources/static/FirebaseAdminSDKJava.json");

    GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);

    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(credentials)
        .build();

    FirebaseApp.initializeApp(options);

    Firestore db = FirestoreClient.getFirestore();

     @RequestMapping("/")
     public String index() {
         return "This is the index!\n";
     }

     @RequestMapping("/hello")
     public String index2() {

         return "Hello, World!\n";
     }
}

I am using Spring framework.

FirebaseAdminSDKJava.json located: src -> main -> resources -> static -> FirebaseAdminSDKJava.json

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Xose
  • 85
  • 2
  • 9
  • 1
    You don't have to guess which line the error is on. The stack trace will give you the line number. Consider post the entire stack trace and point out which line the error is actually on. – Doug Stevenson Jan 04 '20 at 18:49
  • 1
    @DougStevenson: This sounds like a compile-time error, not an exception being thrown - there won't be any stack trace. – Jon Skeet Jan 04 '20 at 19:25

2 Answers2

0

"/src/main/resources/static/FirebaseAdminSDKJava.json" is a relative file path. Depending on where your application is running, this file may or may not be in that position. Once built and deployed, the "src" dir won't be there, only in development.

Instead, load this file from the classpath. See this answer: How to really read text file from classpath in Java

KevinB
  • 1,102
  • 2
  • 8
  • 19
  • Now Eclipse shows IOException at this line:`GoogleCredentials credentials = GoogleCredentials.fromStream(serviceAccount);` – Xose Jan 04 '20 at 19:16
0

Classpath includes what you have inside you resources directory. please try this

1. InputStream in = this.getClass().getClassLoader()
                                .getResourceAsStream("FirebaseAdminSDKJava.json");


2 classpath:static/FirebaseAdminSDKJava.json

For debugging, 1. start eclipse in debugger mode or put your code in try block and instead of catching FileNotFoundException catch parent exception "Exception".

sample ex

try{
   //your code
}catch(Exception ex){
  ex.printStackTrace();
}

printStackTrace() will show us the actual root cause of the issue

hope it will work for you

Sarjit
  • 799
  • 7
  • 9