Skip to main content

EJBs with Guava Libraries conflict with Embedded Glassfish

3 replies [Last post]
biggus
Offline
Joined: 2012-03-16
Points: 0

I have created a very simple maven experiment containing one Singleton bean. The project consists of a parent pom, an ear, and an ejb jar that depends on guava 11.0.2 (see attachment).

When I execute this with the goal integration-test, I get the error java.lang.NoSuchMethodError: com.google.common.collect.ImmutableSet.copyOf(Ljava/util/Collection;)Lcom/google/common/collect/ImmutableSet;

I have seen the solution involving <class-loader delegate="false" /> at stackoverflow.com, but I'm not sure if this even applies to a non-WAR project.

I understand this may be because embedded glassfish has a conflicting version of guava. How can I make this experiment work?

The EJB class:

package com.acme.guavaEJB;

import static com.google.common.collect.Lists.newArrayList;

import java.util.List;
import java.util.Set;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

import com.google.common.collect.ImmutableSet;

@Singleton
@Startup
public class GuavaService {

@PostConstruct
public void test() {
List<String> colors = newArrayList("red", "green", "blue");

Set<String> colorSet = ImmutableSet.copyOf(colors);

System.out.println(colorSet.size());
}

}

Parent pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.acme</groupId>
  <artifactId>malachite</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>malachite</name>
  <modules>
    <module>guavaEAR</module>
    <module>guavaEJB</module>
  </modules>
</project>

EAR pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>malachite</artifactId>
<groupId>com.acme</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>guavaEAR</artifactId>
<packaging>ear</packaging>

<name>guavaEAR</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.6</version>
<configuration>
<version>6</version>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<modules>
<ejbModule>
<groupId>com.acme</groupId>
<artifactId>guavaEJB</artifactId>
</ejbModule>
</modules>
</configuration>
</plugin>
<plugin>
<groupId>org.glassfish.embedded</groupId>
<artifactId>maven-embedded-glassfish-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<goalPrefix>embedded-glassfish</goalPrefix>
<app>${project.build.directory}/${project.build.finalName}.ear</app>
<port>8888</port>
<ports>
<https-listener>8989</https-listener>
</ports>
<name>${project.build.finalName}</name>
<serverID>guavaEAR-embedded</serverID>
<glassfishProperties></glassfishProperties>
</configuration>
<executions>
<execution>
<id>run</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>guavaEJB</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>ejb</type>
</dependency>
</dependencies>
</project>

EJB pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>malachite</artifactId>
<groupId>com.acme</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>guavaEJB</artifactId>
<packaging>ejb</packaging>

<name>guavaEJB</name>

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>11.0.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>

</project>
AttachmentSize
malachite.zip5.98 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tandres19
Offline
Joined: 2011-01-05
Points: 0

I think I have a very similar problem (except that I try to use a newer weld version instead of an older one) and see the problem in the running glassfish server (not just eh embedded one)

See https://java.net/jira/browse/GLASSFISH-20850

Anyone got an idea how to solve this?

mgainty
Offline
Joined: 2004-05-21
Points: 0

googlecode is a gigantic swamp with many hungry alligators..on top there is no JIRA so caveat emptor

Which version of guava are you implementing?
What is the specific error from the log?

Martin
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.

Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.

> To: users@glassfish.java.net
> Subject: Re: EJBs with Guava Libraries conflict with Embedded Glassfish
> From: forums@java.net
> Date: Fri, 11 Oct 2013 06:32:05 -0500
>
> I think I have a very similar problem (except that I try to use a newer weld
> version instead of an older one) and see the problem in the running glassfish
> server (not just eh embedded one) See
> https://java.net/jira/browse/GLASSFISH-20850 Anyone got an idea how to solve
> this?
>
> --
>
> [Message sent by forum member 'tandres19']
>
> View Post: http://forums.java.net/node/884793
>
>

tandres19
Offline
Joined: 2011-01-05
Points: 0

I'm not implementing guava. I use guave-15.0, as I've written in the referenced JIRA.

Description from the JIRA:
I tried to upgrad the guava library in our application to version 15-0. When I deploy the application on glassfish 4.0, I see the following Exception:

Caused by: java.lang.NoSuchMethodError: com.google.common.base.Stopwatch.createStarted()Lcom/google/common/base/Stopwatch;
at MyClient.buildClient(MyClient.java:159)
at MyClient.initClient(MyClient.java:143)
at MyClient.(MyClient.java:74)
at MyClient.(MyClient.java:62)
at MyClientFactory.createClient(MyClientFactory.java:12)
at MyClientProducer.createClient(MyClientProducer.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93)

To verify where the class is loaded from, I added:
URL resource = this.getClass().getClassLoader()
.getResource(Stopwatch.class.getName().replaceAll("
.", "/") + ".class");
LOGGER.info("URL for Stopwatch:" + resource.toString());
which gives me
URL for Stopwatch:bundle://108.0:1/com/google/common/base/Stopwatch.class

MyClient, MyClientFactory and MyClientProducer are all inside an ejb jar file.
guava-15.0.jar is in a lib folder next to the ejb jar (with lib/guava-15.0.jar in the ear META-INF/MANIFEST.MF Class-Path)

All other libraries there can be found. It just seems, that the weld classloader favors the guava.jar bundled with glassfish 4 over the one I provide for the application. I would expect the classloader to give stuff deployed with the ear a higher priority.