Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: StringUtils.join on an ArrayList returns NoSuchMethodError Exception

I have an ArrayList that i would like to join with a delimiter of ',', i read in some answers here that StringUtils.join is a good option but the problem is that when i try to join an ArrayList i get the following error:

java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.join(Ljava/util/Collection;C)Ljava/lang/String;

code:

ArrayList<String> friendsList = new ArrayList<String>();
.
.
.
StringUtils.join(friendsList, ',');

what am i missing ?

when i'm coding with netbeans it does not alert me of this error, it happens only when I try to compile.

like image 853
ufk Avatar asked Aug 04 '10 15:08

ufk


4 Answers

You have an older version of commons-lang. Get the latest version, which has this method.

Alternatively, you can call StringUtils.join(friendsList.toArray(), ',')

like image 192
Bozho Avatar answered Oct 18 '22 13:10

Bozho


"it happens only when I try to compile."

This is not a compilation error. It's a linkage error that happens at runtime when the signature of the method being invoked does not match the one of the relevant class in the classpath. You probably have different jars at compile time and runtime (different versions maybe).

like image 21
Eyal Schneider Avatar answered Oct 18 '22 12:10

Eyal Schneider


An issue with classpath I guess.

like image 1
Zaki Avatar answered Oct 18 '22 13:10

Zaki


This method exists since commons lang 2.3, check your jar.

like image 1
Antoine Avatar answered Oct 18 '22 13:10

Antoine