Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

At the other end of the link I post some results of my other hobbies, like playing bass. It is worth checking it out ;)

Glassfish5 and Firebase — The Guava Problem

I tried to deploy a simple Java EE 7 application to Glassfish5 during the weekend, and I got an exception because of Guava. The error message is not telling clearly that Guava is the root cause.

The exception

Without much introduction, you get this error message if you try to deploy an application to Glassfish5 which is using Firebase:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
	at com.google.firebase.FirebaseApp.checkNotDeleted(FirebaseApp.java:314)

The cause

This exception is caused merely because of the Guava version. I didn’t know this either until I googled and found this issue with Firebase. Here you can read, that this problem is caused by the different versions of Guava: one coming with Firebase, another already available with Glassfish.

The solution

Solving this problem requires some steps to do:

  1. Exclude Guava from Firebase
  2. Add Guava as a dependency to your project
  3. Update Guava in Glassfish

Excluding Guava from Firebase

This action is necessary to solve the “two Guavas on the classpath” problem. Because you already have Guava in your Glassfish, you can exclude it from the dependencies of Firebase. And with this step, you make sure that updating Firebase doesn’t invoke the same problem.

You can exclude Guava in a Maven project like this:

<dependency>
    <groupId>com.google.firebase</groupId>
    <artifactId>firebase-admin</artifactId>
    <version>5.7.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Or if you are using Gradle:

compile ('com.google.firebase:firebase-admin:5.7.0') {
    exclude group: 'com.google.guava'
}

Adding Guava as a dependency to your project

Now you need to add Guava to your project as a dependency.

If you are using Maven:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.6-jre</version>
</dependency>

Or for Gradle:

compile 'com.google.guava:guava:23.6-jre'

Updating Guava in Glassfish

Now your project uses the actual Guava version, but Glassfish uses the same old version which needs an update. This is because it’ using a very-very old version which doesn’t have some methods Firebase needs.

You can find the culprit at the following location:

<glassfish installation directory>/glassfish/modules/guava.jar

If you look into its contents (rename it to guava.zip), then you can see in the pom.xml the following:

<parent>
    <groupId>com.google.guava</groupId>
    <artifactId>guava-parent</artifactId>
    <version>13.0.1</version>
 </parent>

13.0.1 is really old.

To change this guava to the same you are using in your project as a dependency, just download the JAR file from Maven Central, rename is to guava.jar and replace the old version with this one. If your server is running, restart it.

Share the knowledge!
GHajba
 

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below 0 comments
>
%d bloggers like this: