Adding the suggested line(s):
CountDownLatch latch = new CountDownLatch(1);
..
latch.countDown();
latch.await();
surrounding the database query exposes the following exception after ant jar from Netbeans:
-do-jar:
jar:
BUILD SUCCESSFUL
Total time: 2 seconds
thufir@doge:~/NetBeansProjects/Firebase$
thufir@doge:~/NetBeansProjects/Firebase$ java -jar dist/
Firebase.jar lib/
thufir@doge:~/NetBeansProjects/Firebase$ java -jar dist/Firebase.jar
Exception in thread "pool-4-thread-1" java.lang.NoClassDefFoundError: com/google/api/client/auth/oauth2/Credential
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.google.firebase.auth.FirebaseCredentials$CertCredential.fetchCredential(FirebaseCredentials.java:276)
at com.google.firebase.auth.FirebaseCredentials$BaseCredential$1.call(FirebaseCredentials.java:229)
at com.google.firebase.auth.FirebaseCredentials$BaseCredential$1.call(FirebaseCredentials.java:224)
at com.google.firebase.tasks.Tasks$1.run(Tasks.java:78)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.google.api.client.auth.oauth2.Credential
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 19 more
^Cthufir@doge:~/NetBeansProjects/Firebase$
thufir@doge:~/NetBeansProjects/Firebase$
thufir@doge:~/NetBeansProjects/Firebase$ ll dist/lib/
total 5024
drwxrwxr-x 2 thufir thufir 4096 Jul 3 20:19 ./
drwxrwxr-x 3 thufir thufir 4096 Jul 3 20:18 ../
-rw-rw-r-- 1 thufir thufir 648491 Jul 3 20:19 firebase-admin-5.2.0.jar
-rw-rw-r-- 1 thufir thufir 430683 Jul 3 20:19 firebase-client-jvm-2.2.3.jar
-rw-rw-r-- 1 thufir thufir 610019 Jul 3 20:19 firebase-server-sdk-3.0.3.jar
-rw-rw-r-- 1 thufir thufir 199100 Jul 3 20:19 google-api-client-1.22.0.jar
-rw-rw-r-- 1 thufir thufir 239194 Jul 3 20:19 google-api-client-1.4.1-beta.jar
-rw-rw-r-- 1 thufir thufir 16799 Jul 3 20:19 google-api-client-auth-oauth2-1.2.3-alpha.jar
-rw-rw-r-- 1 thufir thufir 6720 Jul 3 20:19 google-http-client-jackson2-1.22.0.jar
-rw-rw-r-- 1 thufir thufir 2575022 Jul 3 20:19 guava-22.0.jar
-rw-rw-r-- 1 thufir thufir 316575 Jul 3 20:19 jackson-core-2.9.0.pr4.jar
-rw-rw-r-- 1 thufir thufir 15896 Jul 3 20:19 jackson-jaxrs-json-provider-2.9.0.pr4.jar
-rw-rw-r-- 1 thufir thufir 57264 Jul 3 20:19 json-20170516.jar
thufir@doge:~/NetBeansProjects/Firebase$
code:
package dur.bounceme.net.firebase;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.auth.FirebaseCredentials;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
public class FirebaseQuery {
private static final Logger log = Logger.getLogger(FirebaseQuery.class.getName());
private DatabaseReference databaseReference = null;
private Properties firebaseProperties = null;
public FirebaseQuery() {
}
void setProperties(Properties firebaseProperties) {
this.firebaseProperties = firebaseProperties;
}
void pushUsers(List<User> users) {
for (User u : users) {
databaseReference.child("users").child(u.uuid.toString()).setValue(u);
}
}
void tryQuery() {
try {
query();
} catch (IOException ex) {
Logger.getLogger(FirebaseQuery.class.getName()).log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) {
Logger.getLogger(FirebaseQuery.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void query() throws FileNotFoundException, IOException, InterruptedException {
String url = firebaseProperties.getProperty("url");
FileInputStream serviceAccount = new FileInputStream("serviceAccountKey.json");
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl(url)
.build();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(options);
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance(firebaseApp, url);
databaseReference = firebaseDatabase.getReference().getRoot();
CountDownLatch latch = new CountDownLatch(1);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String key = null;
String value = null;
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
key = childSnapshot.getKey();
value = (String) childSnapshot.getValue();
log.info(key + "\t\t" + value);
}
latch.countDown();
}
@Override
public void onCancelled(DatabaseError databaseError) {
latch.countDown();
throw databaseError.toException();
}
});
latch.await();
}
}
So far as I can tell these are the correct packages, but apparently not because there's no Credential class:
thufir@doge:~/NetBeansProjects/Firebase$
thufir@doge:~/NetBeansProjects/Firebase$ jar -tf dist/lib/google-api-client-auth-oauth2-1.2.3-alpha.jar
META-INF/
META-INF/MANIFEST.MF
com/
com/google/
com/google/api/
com/google/api/client/
com/google/api/client/auth/
com/google/api/client/auth/oauth2/
com/google/api/client/auth/oauth2/AccessProtectedResource$AccessTokenIntercepter.class
com/google/api/client/auth/oauth2/AccessProtectedResource$UsingAuthorizationHeader.class
com/google/api/client/auth/oauth2/AccessProtectedResource$UsingQueryParameter.class
com/google/api/client/auth/oauth2/AccessProtectedResource$UsingFormEncodedBody.class
com/google/api/client/auth/oauth2/AccessProtectedResource.class
com/google/api/client/auth/oauth2/AccessTokenRequest$AuthorizationCodeGrant.class
com/google/api/client/auth/oauth2/AccessTokenRequest$ResourceOwnerPasswordCredentialsGrant.class
com/google/api/client/auth/oauth2/AccessTokenRequest$AssertionGrant.class
com/google/api/client/auth/oauth2/AccessTokenRequest$RefreshTokenGrant.class
com/google/api/client/auth/oauth2/AccessTokenRequest.class
com/google/api/client/auth/oauth2/AccessTokenErrorResponse$KnownError.class
com/google/api/client/auth/oauth2/AccessTokenErrorResponse.class
com/google/api/client/auth/oauth2/AccessTokenResponse.class
com/google/api/client/auth/oauth2/AuthorizationRequestUrl$ResponseType.class
com/google/api/client/auth/oauth2/AuthorizationRequestUrl.class
com/google/api/client/auth/oauth2/AuthorizationResponse$KnownError.class
com/google/api/client/auth/oauth2/AuthorizationResponse.class
META-INF/maven/
META-INF/maven/com.google.api.client/
META-INF/maven/com.google.api.client/google-api-client-auth-oauth2/
META-INF/maven/com.google.api.client/google-api-client-auth-oauth2/pom.xml
META-INF/maven/com.google.api.client/google-api-client-auth-oauth2/pom.properties
thufir@doge:~/NetBeansProjects/Firebase$
where can I download the JAR with this specific class? Exception occurs regardless of whether run from the CLI through ant run, running the JAR directly as above, or using Netbeans.
Class added in verson 1.7 see java docs
Upgrade your jar. See download
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With