0

Currently I am setting up a Firebase cloud Firestore in java, using IntelliJ as SDK. In the Firebase documentation is said that it is needed the Service Account Key which is a json file. I used the method FileInputStream to obtain this json file, and I do not have any problem when I am getting that file executing the program, but when I export this as a JAR library the project which uses that JAR does not find the Service Account Key, and then it can not connect with my firebase.

Now, I am able to connect to firebase but I want to do all that process on a Library that I am doing. I show you the code that is getting my file.

public InputStream obtainInputStream(String pathFile) throws ReadFileException {

        InputStream inputStream;

        try {
            // Create File Input Stream
            inputStream = new FileInputStream(pathFile);

        } catch (FileNotFoundException e) {
            // Get Input Stream
            inputStream = getClass().getResourceAsStream(pathFile);

            // Check if File Exists
            if(inputStream == null) {

                throw new ReadFileException(String.format("Error Find File %s", pathFile));

            }
        }
        return inputStream;
    }

When I export all my code as a JAR file it does not found the file and show a FileNotFoundException, even when I save the file in the resources folder. Could I do anything in the build gradle file or use an equivalent method? I want to use this project as a Libray in other project.

snieguu
  • 2,073
  • 2
  • 20
  • 39
  • What is the value of "pathFile" when this method gets called? Where is the file located in your file system? Where is the file located inside your jar (you can use "jar tvf my.jar" to see where the file is inside your jar. [use the correct name for "my.jar"])? – StvnBrkdll Nov 06 '19 at 15:35
  • It is used in other project to validate a user. The path file is "/ServiceAccount.json" which is a key that firebase give us to acces to their services. In my file system it is located in a root folder which is created by intelliJ and finally, it is in the root of my jar file. – andres.olaya04 Nov 06 '19 at 16:58
  • You certainly cannot use a FileInputStream to read a .jar entry. A .jar file is a zip archive and its entries are not individual files. Check the contents of the .jar file and verify that ServiceAccount.json is present in it. – VGR Nov 06 '19 at 17:52
  • I don't want to read my jar file with FileInputStream. Also, I check the contents and my file is into the jar File, but the error remains. – andres.olaya04 Nov 06 '19 at 19:14
  • @andres.olaya04 In order to solve your problem, you need to understand the difference between a file and a resource in java. This SO post will help you understand how to read a resource. In your case, because ServiceAccount.json is packaged in your jar file, it is a resource. You access resources differently than you access files. https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar – StvnBrkdll Nov 07 '19 at 15:24

1 Answers1

0

Hello i would recommend you to export the api key and the data of firebase connection as a set of environment variables, that will make the project easy to export and easy to read and change in other machines where you put the jar project.

you have to creaste a .env in the root of the project and use the library dotenv

example :

in .env file define the vars

 # formatted as key=value
 MY_ENV_VAR1=some_value
 MY_EVV_VAR2=some_value

in the java file you can get that variables like this

import io.github.cdimascio.dotenv.Dotenv;
Dotenv dotenv = Dotenv.load();
dotenv.get(“MY_ENV_VAR1”);
Jesus Vega
  • 9
  • 1
  • 1