I don't know why it doesn't work. I was just making my own application with this.
I used some from quickstart repository in firebase github. Also, I added System.out.println
in the listener at private static void get but that doesn't work too. I think that listener was not executed.
Here's my code:
package me.JacobLim.test;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.database.*;
import org.apache.log4j.BasicConfigurator;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
//Log4j Configurator
BasicConfigurator.configure();
try {
System.out.println("Started Main");
FileInputStream serviceAccount = new FileInputStream("Json Path");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setDatabaseUrl("https://<My Project :)>-default-rtdb.firebaseio.com")
.build();
FirebaseApp.initializeApp(options);
Thread.sleep(2000);
System.out.println("Finished Init");
} catch (IOException e) {
System.out.println("ERROR: invalid service account credentials. See README.");
System.out.println(e.getMessage());
System.exit(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("a");
System.out.println(reference.getPath());
get(reference);
}
private static void get(DatabaseReference reference) {
System.out.println("Start get");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println(dataSnapshot.getValue(String.class));
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("ERROR");
}
});
}
}
Here's log:
Started Main Finished Init 0
[main] DEBUG com.google.firebase.FirebaseApp - Starting the proactive token refresher 2
[main] DEBUG com.google.firebase.FirebaseApp - Scheduling next token refresh in 0 milliseconds 9
[firebase-scheduled-worker] DEBUG com.google.firebase.FirebaseApp - Refreshing OAuth2 credential
/a Start get