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
"/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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.