0

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 enter image description here

and this is the message under next to it: enter image description here

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

1 Answers1

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);
            }
        }
    }
 }
Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38