Eshaka
Eshaka

Reputation: 994

Firebase admin sdk with java doesn't connect

I am trying to use firebase database to create a java desktop RMI data management system. My first step was to confirm the connectivity of firebase using the admin sdk. I followed the step in the guide exactly as it is and got no progress. the program executes, but there is no data modification in my firebase console.

following is the code i have so far..

public class Main{

    public static class User2 {

        public String date_of_birth;
        public String full_name;
        public String nickname;

        public User2(String dateOfBirth, String fullName) {
            this.date_of_birth = dateOfBirth;
            this.full_name = fullName;
        }

        public User2(String dateOfBirth, String fullName, String nickname) {
            this.date_of_birth = dateOfBirth;
            this.full_name = fullName;
            this.nickname = nickname;
        }
    }

    public static void main(String[] args) {

        FileInputStream serviceAccount = null;
        try {

            serviceAccount = new FileInputStream("rathnapuralabs-firebase-adminsdk-okzic-f6313557b4.json");
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }

        FirebaseOptions options = null;
        try {

            options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                    .setDatabaseUrl("https://rathnapuralabs.firebaseio.com")
                    .build();
        } catch (IOException e) {

            e.printStackTrace();
        }
        FirebaseApp.initializeApp(options);

        DatabaseReference ref = FirebaseDatabase.getInstance()
                .getReference();

        DatabaseReference usersRef = ref.child("users2");

        Map<String, User2> users = new HashMap<>();
        users.put("alanisawesome", new User2("June 23, 1912", "Alan Turing"));
        users.put("gracehop", new User2("December 9, 1906", "Grace Hopper"));

        usersRef.setValueAsync(users);

        ref.addListenerForSingleValueEvent(new ValueEventListener() {
           @Override
           public void onDataChange(DataSnapshot dataSnapshot) {
               Object document = dataSnapshot.getValue();
               System.out.println(document);
           }

           @Override
           public void onCancelled(DatabaseError error) {
           }
    });
    }

this code give me three 3 lines of errors.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

but something tells me that even with these errors the data still should be saved to the firebase.

following is the github link to the codes and the key json.

https://github.com/bandarawaththa/Testing-Firebase-with-realtime-db.git

Upvotes: 1

Views: 395

Answers (1)

Constantin Beer
Constantin Beer

Reputation: 5835

The following code worked for me:

try {
            FirebaseOptions options = new FirebaseOptions.Builder()
                    .setCredentials(GoogleCredentials
                                    .fromStream(new ClassPathResource("/firebase-authentication.json").getInputStream()))
                    .build();

            if (FirebaseApp.getApps().isEmpty()) {
                FirebaseApp.initializeApp(options);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

The "/firebase-authentication.json" is in the resources folder.

Upvotes: 1

Related Questions