简体   繁体   中英

Export FireBase With gradle (RealTime DataBase)

I want export my project of firebase to jar, and when I try use the jar, it doesn't work.

My build.gradle:

apply plugin: "java"

buildscript {
    ext.bukkit_version = "1.8.3-R0.1-SNAPSHOT"
}

repositories {
    mavenCentral()
    maven {
        url "https://hub.spigotmc.org/nexus/content/repositories/snapshots/"
    }
}

version = "0.1"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"

configurations {
    shade
    compile.extendsFrom shade
}

dependencies {
    compile "org.bukkit:bukkit:$bukkit_version"
    shade 'com.google.firebase:firebase-admin:6.12.2'
}

jar {
    configurations.shade.each { dep ->
        from(project.zipTree(dep)) {
            exclude 'META-INF', 'META-INF/**'
        }
    }
}

I use this code to check what is the problem:

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println(1);
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream(new FileInputStream(new File("../yourpixel/YourPixel/key.json"))))
        .setDatabaseUrl("https://yourpixel-22c2a.firebaseio.com/")
        .build();
    System.out.println(2);
    FirebaseApp.initializeApp(options);
    System.out.println(3);
    CountDownLatch latch = new CountDownLatch(1);
    FirebaseDatabase.getInstance().getReference("TEST").setValue("2", (databaseError, databaseReference) -> {
        System.out.println("Error:" + (databaseError == null ? "null" : databaseError.getMessage()));
        System.out.println("Reference:" + (databaseReference == null ? "null" : databaseReference.getPath()));
        latch.countDown();
    });
    System.out.println(4);
    latch.await();
    System.out.println(5);
}

When I run this inside my project, it works and the output is:

1
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.
2
3
4
Error:null
Reference:/TEST

When I export my project to jar and use it to run the same main, it doesn't work. The output is:

1
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.
2
3
4

And the program stuck.

This is a very bad idea in any program:

while (true);

This is called a tight loop, and it will completely tie up the CPU on the thread where you run this. And since the completion handler for setValue also needs to run on that thread, it is never able to execute.

To properly wait for the data to be written, you'll want to use semaphores to signal when the writing is done. For example, with a CountdownLatch :

public static void main(String[] args) throws IOException {
    FirebaseOptions options = new FirebaseOptions.Builder()
        .setCredentials(GoogleCredentials.fromStream(new FileInputStream(new File("key.json"))))
        .setDatabaseUrl("https://yourpixel-22c2a.firebaseio.com/")
        .build();

    FirebaseApp.initializeApp(options);
    CountDownLatch latch = new CountDownLatch(1);

    FirebaseDatabase.getInstance().getReference("TEST").setValue("2", (databaseError, databaseReference) -> {
        latch.countDown();
    });

    latch.await();
}

I fixed my problam. (I didn't said that i use intellij) The problem was that the project that use the jar, had anoter jar. i dragged FireBase.jar (the jar i made) to over craftbukkit (like in the picture) and it fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM