Michael Michael - 1 year ago 75
Java Question

Java method not found in ColdFusion

I am implementing GoCardless into my ColdFusion application by using their Java library and have encountered the following error:

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String([B)Ljava/lang/String;

As explained in the GoCardless documentation their library is dependent upon the Apache Commons Codec library (amongst others). When I put the GoCardless JAR into my ColdFusion lib folder I noticed that the Apache Commons Codec JAR is already present and so am unsure what this error signifies.

Any help would be much appreciated. Thanks!

Answer Source

Assuming you are not invoking that method, it probably means exactly what it says. The codec version loaded by the jvm does not contain a method with that signature. You can verify it by creating an instance of org.apache.commons.codec.binary.Base64 class and checking the available methods with cfdump. (It might also be a class loader conflict, but the former is more likely.)

As far as the reason, when multiple versions of a jar are present in the class path, the jvm can only pick one of them. It probably picked the older version that ships with ColdFusion, which may not have that method. See: Identifying which jar a class was loaded from...

The simplest way to load a newer jar version is to remove the old jar in /lib, replace it with the newer version, and restart CF. Keep in mind that library is also used by CF itself, so you run the risk of breaking something if the newer version is not backward compatible. Another option is using CF10's dynamic class loading feature. It is basically a rip of Mark Mandel's JavaLoader.cfc, just bundled into CF.