How do I correctly add the file input stream to my Firebase JSON file [duplicate]

会有一股神秘感。 提交于 2019-12-12 03:54:46

问题


My JSON file resides in the projects root folder, I have correctly set up the dependencies and now I'm trying to add the JSON file Using the instructions as provided by Firebse themselves here.

Here is the code from my main java file:

import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.management.RuntimeMXBean;
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.LineNumberReader;
import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;
import java.lang.management.ManagementFactory;
import java.util.Random;

 public class Climate {
    public static void main(String [] args){
          FileInputStream serviceAccount = new FileInputStream("serviceAccountKey.json");


          FirebaseOptions options = new FirebaseOptions.Builder()
                .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                .setDatabaseUrl("https://*retracted*.firebaseio.com/")
                .build();

          FirebaseApp.initializeApp(options);
    }
 }

this is how it look in the IDE

and this is the message under next to it:

It says file not found but the file is there i assure you, am I being really dumb here, please excuse my incompetence I'm new to firebase... any help would be much appreciated.


回答1:


You need to surround that statement by "try and catch" or use throws exceptions.

 public class Climate {
    public static void main(String [] args){
          FileInputStream serviceAccount = null;
        try {
            serviceAccount = new FileInputStream("serviceAccountKey.json");
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
                    .setDatabaseUrl("https://*retracted*.firebaseio.com/")
                    .build();
            FirebaseApp.initializeApp(options);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Climate.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                serviceAccount.close();
            } catch (IOException ex) {
                Logger.getLogger(Climate.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
 }


来源:https://stackoverflow.com/questions/44981655/how-do-i-correctly-add-the-file-input-stream-to-my-firebase-json-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!