5

I have a Java Spring Boot backend application that uses Firebase Admin SDK. inside the init method for FirebaseConfig I have to supply the credentials file for the FirebaseOptions,

@PostConstruct
public void init() throws IOException {
    ClassLoader classLoader=Thread.currentThread().getContextClassLoader();
    FileInputStream refreshToken = new FileInputStream("src/main/resources/progresee-fa969-firebase-adminsdk-3vip2-d80bf340b7.json");
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream(refreshToken))
        .setDatabaseUrl(dbUrl)
        .build();
    FirebaseApp.initializeApp(options);
}

everything works fine when I run locally, but when I build the jar file and upload to the server hosting service(AWS) I get a FileNotFound error -

Caused by: java.io.FileNotFoundException: /progresee-fa969-firebase-adminsdk-3vip2-d80bf340b7.json (No such file or directory)
at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_222]
at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_222]
at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_222]
at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[na:1.8.0_222]
at com.progresee.app.firebase.FirebaseConfig.init(FirebaseConfig.java:55) ~[classes!/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_222]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_222]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_222]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_222]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:363) ~[spring-beans-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:307) ~[spring-beans-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-5.1.9.RELEASE.jar!/:5.1.9.RELEASE]
... 74 common frames omitted

I tried to upload the file to a s3 bucket or firebase and to access it via URL but that way doesn't even work locally.

Thanks in advance, Chen.

  • Does this answer your question? [IOException When Trying to Load JSON data file via Spring Boot Command Line Runner](https://stackoverflow.com/questions/58703834/ioexception-when-trying-to-load-json-data-file-via-spring-boot-command-line-runn) – Ryuzaki L Nov 17 '19 at 18:59
  • Still gets the same error – Chen Ben Ami Nov 17 '19 at 21:16

1 Answers1

3

For adding the JSON configuration in the fat jar, please follow these steps.

  1. Place the JSON file in src/main/resources

  2. Inject the resource loader in Service

    @Autowired
    ResourceLoader resourceLoader;
    
  3. Get the inputstream of the resource instead of getting the file

    Resource resource = resourceLoader.getResource("classpath:firebaseFile.json");    
    InputStream inputStream = resource.getInputStream();
    
  4. Pass the inputstream in firebase option

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197