Java EE 应用程序中的 java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String()

发布于 2024-12-08 19:41:44 字数 469 浏览 5 评论 0原文

我正在开发一个 Java EE 应用程序,其中需要 Base64 编码/解码,

因此我在应用程序的 WEB-INF/lib 文件夹中添加了 commons-codec-1.5.jar ,并且 使用

import org.apache.commons.codec.binary.Base64;

在 Java 文件中

。在编译时,当我输入 Base64 时,它显示 encodeBase64String 方法可用。但在运行时它会抛出这样的异常:

java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String

我在构建路径中有 JAR,但我仍然不明白为什么它会抛出上述错误。

I am developing a Java EE application in which I need Base64 Encoding/Decoding

So I added commons-codec-1.5.jar in WEB-INF/lib folder of my application and used

import org.apache.commons.codec.binary.Base64;

in the Java file.

During compile time, when I type Base64, it shows encodeBase64String method is available. But during runtime it is throwing an exception like this:

java.lang.NoSuchMethodError:org.apache.commons.codec.binary.Base64.encodeBase64String

I have the JAR in the buildpath, but still I don't understand why it throws me the above error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

春夜浅 2024-12-15 19:41:44

该方法是 在 Commons Codec 1.4 中引入。此异常表明您在 web 应用程序的运行时类路径中的其他位置有较旧版本的 Commons Codec,该版本在类加载中具有优先权。检查 web 应用程序的运行时类路径覆盖的所有路径。其中包括 Webapp/WEB-INF/libYourAppServer/libJRE/libJRE/lib/ext。最后删除或升级有问题的旧版本。


更新:根据评论,您似乎无法找到它。我只能建议使用较新的方法对代码进行注释,然后将以下行放在适当的位置:

System.out.println(Base64.class.getProtectionDomain().getCodeSource().getLocation());

这应该打印运行时加载 JAR 文件的绝对路径。


更新 2:这似乎确实指向了正确的文件。抱歉,我现在无法再解释您的问题。我所能建议的就是使用不同的 Base64 方法,例如 encodeBase64(byte[]) ,然后自己构造一个 new String(bytes) 。或者您可以删除该库并使用不同的 Base64 编码器,例如这个

That method was introduced in Commons Codec 1.4. This exception indicates that you've an older version of Commons Codec somewhere else in the webapp's runtime classpath which got precedence in classloading. Check all paths covered by the webapp's runtime classpath. This includes among others the Webapp/WEB-INF/lib, YourAppServer/lib, JRE/lib and JRE/lib/ext. Finally remove or upgrade the offending older version.


Update: as per the comments, you can't seem to locate it. I can only suggest to outcomment the code using that newer method and then put the following line in place:

System.out.println(Base64.class.getProtectionDomain().getCodeSource().getLocation());

That should print the absolute path to the JAR file where it was been loaded from during runtime.


Update 2: this did seem to point to the right file. Sorry, I can't explain your problem anymore right now. All I can suggest is to use a different Base64 method like encodeBase64(byte[]) and then just construct a new String(bytes) yourself. Or you could drop that library and use a different Base64 encoder, for example this one.

李不 2024-12-15 19:41:44

一些 Google 工具(例如 GWT)具有嵌入版本的 commons-codec,带有 1.4 之前的 Base64 类。您可能需要通过重构项目来使代码无法访问此类工具 JAR,以便只有需要该工具的代码部分才能看到依赖项。

Some Google tooling such as GWT has an embedded version of commons-codec with a pre-1.4 Base64 class. You may need to make such tooling JARs inaccessible to your code by refactoring your project such that only the parts of your code that need that tooling can see the dependency.

软甜啾 2024-12-15 19:41:44

@Adam Augusta 是对的,还有一件事

Apache-HTTP 客户端 jar 也与一些 google-api 属于同一类别。

org.apache.httpcomponents.httpclient_4.2.jar
和commons-codec-1.4.jar 都在类路径上,这很可能你会遇到这个问题。

这证明了所有在内部使用早期版本的 common-codec 的 jar,同时也有人在 classpath 上显式使用 common-codec

@Adam Augusta is right, One more thing

Apache-HTTP client jars also comes in same category as some google-apis.

org.apache.httpcomponents.httpclient_4.2.jar
and commons-codec-1.4.jar both on classpath, This is very possible that you will get this problem.

This prove to all jars which are using early version of common-codec internally and at the same time someone using common-codec explicitly on classpath too.

久伴你 2024-12-15 19:41:44

下载 这个 jar

它解决了我的问题,这是 1.7。

Download this jar

It resolved my problem, this is 1.7.

风铃鹿 2024-12-15 19:41:44

在部署 Web 应用程序时,我在 JBoss 4.2.3 GA 上遇到了同样的问题。我通过将 commons-codec 1.6 jar 复制到 C:\jboss-4.2.3.GA\server\default\lib 解决了这个问题

I faced the same problem with JBoss 4.2.3 GA when deploying my web application. I solved the issue by copying my commons-codec 1.6 jar into C:\jboss-4.2.3.GA\server\default\lib

夏至、离别 2024-12-15 19:41:44

您的类路径中需要 Apache Commons Codec 库 1.4 或更高版本。
该库包含 Base64 实现。

You need the Apache Commons Codec library 1.4 or above in your classpath.
This library contains Base64 implementation.

第几種人 2024-12-15 19:41:44

尝试将“commons-codec-1.8.jar”添加到您的 JRE 文件夹中!

Try add 'commons-codec-1.8.jar' into your JRE folder!

小草泠泠 2024-12-15 19:41:44

时,只需创建一个Base64的对象并用它来编码或解码

当使用org.apache.commons.codec.binary.Base64库进行编码

Base64 ed=new Base64();

字符串编码=new String(ed.encode("Hello".getBytes()));

将“Hello”替换为要以字符串格式编码的文本。

解码

Base64 ed=new Base64();

字符串解码=new String(ed.decode(encoded.getBytes()));

这里encoded是要解码的String变量

Simply create an object of Base64 and use it to encode or decode, when using org.apache.commons.codec.binary.Base64 library

To Encode

Base64 ed=new Base64();

String encoded=new String(ed.encode("Hello".getBytes()));

Replace "Hello" with the text to be encoded in String Format.

To Decode

Base64 ed=new Base64();

String decoded=new String(ed.decode(encoded.getBytes()));

Here encoded is the String variable to be decoded

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文