Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with classpath conflict

In my project, I'm using both glassfish-embedded 3.1.1 and the guava lib... The problem is that glassfish comes with the old implementation of guava (ie google-collections)...

This results in NoSuchMethodError at runtime, for example when using Lists.reverse(), or Sets.newIdentityHashSet() which were introduced later in guava.

I don't find any solution to solve this... (the only one is to manually trash the com/google classes directory in the glassfish jar...)

like image 872
sly7_7 Avatar asked Sep 30 '11 16:09

sly7_7


People also ask

How do I fix a classpath conflict?

You will have to make sure that only one library is present on classpath. Simplest way is to create 2 lib directories with correct dependencies and reference all jars from there in your startup script for respective process.

What is a classpath and how do I set it?

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

How do I know if a jar is classpath?

A pragmatic way: Class. forName("com. myclass") where com. myclass is a class that is inside (and only inside) your target jar; if that throws a ClassNotFoundException , then the jar is not on you current classpath.


2 Answers

You can overcome this be specifying the below. Read the Delegation section of the Class Loaders chapter.

<class-loader delegate="false" />

Check this SO post for class loading in Java EE apps in general: Java EE class loading standard

like image 138
Aravind Yarram Avatar answered Sep 24 '22 14:09

Aravind Yarram


Glassfish should never have included com.google classes in their own jar. That was an error.

UPDATE[@sly7_7]: It seems like glassfish does not include guava in their own jar anymore, but it depends on the guava artifact instead. This should resolve the problem. Thanks to @JagWire for pointing this.

like image 35
Kevin Bourrillion Avatar answered Sep 24 '22 14:09

Kevin Bourrillion