英文:
java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64: from a library jar imported with Maven
问题
我制作了一个简单的库jar
,其中包含这个单一的类:
import org.apache.commons.codec.binary.Base64;
public class NiceEncoder {
public static String encode(String first, String second) {
String encoded = new String(Base64.encodeBase64((first + ":" + second).getBytes()));
return encoded;
}
}
它有以下pom.xml
:
<?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>base64tester</groupId>
<artifactId>base64tester</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
然后我将其打包成一个jar
文件并安装到本地Maven存储库:
mvn install:install-file -Dfile=/home/base64tester-1.0-SNAPSHOT.jar -DgroupId=base64tester -DartifactId=base64tester -Dversion=1.0-SNAPSHOT -Dpackaging=jar
然后我在另一个项目中导入它,如下所示:
<dependencies>
<dependency>
<groupId>base64tester</groupId>
<artifactId>base64tester</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
并尝试运行这段代码:
public class TestNiceEncoder {
public static void main(String[] args) {
String first = "some word";
String second = "other word";
System.out.println(NiceEncoder.encode(first, second));
}
}
我得到了java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
错误。
为什么?我该如何修复它,除了在具有TestNiceEncoder
类的项目中导入commons-codec
之外?我该如何确保拥有base64tester.jar
的每个人都可以在没有错误或附加操作的情况下运行它?
编辑:在base64tester
项目中使用其他依赖项也是同样的情况。我让它使用JSoup
库,然后如果从外部项目调用使用它的方法,就会出现异常:
在项目B中:
public class TestNiceEncoder {
public static void main(String[] args) {
String first = "some word";
String second = "other word";
// System.out.println(NiceEncoder.encode(first, second));
System.out.println(NiceEncoder.getRedditTitle()); //在这里发生jsoup错误
}
}
在项目A中(base64tester):
public class NiceEncoder {
public static String encode(String first, String second) {
String encoded = new String(Base64.encodeBase64((first + ":" + second).getBytes()));
return encoded;
}
public static String getRedditTitle() {
try {
Document doc = Jsoup.connect("http://reddit.com/").get();
String title = doc.title();
System.out.println("title: " + title);
return title;
}
catch (Exception e) {
System.out.println("无法打开网址");
return "";
}
}
}
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
</dependencies>
错误是这样的:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
英文:
I've made a simple library jar that consists of this single class:
import org.apache.commons.codec.binary.Base64;
public class NiceEncoder {
public static String encode(String first, String second) {
String encoded = new String(Base64.encodeBase64((first + ":" + second).getBytes()));
return encoded;
}
}
It has this pom.xml
:
<?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>base64tester</groupId>
<artifactId>base64tester</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
</project>
Then I package it into a jar
and install it in local maven repository:
mvn install:install-file -Dfile=/home/base64tester-1.0-SNAPSHOT.jar -DgroupId=base64tester -DartifactId=base64tester -Dversion=1.0-SNAPSHOT -Dpackaging=jar
Then I import it in another project like this:
<dependencies>
<dependency>
<groupId>base64tester</groupId>
<artifactId>base64tester</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
and try running this code:
public class TestNiceEncoder {
public static void main(String[] args) {
String first = "some word";
String second = "other word";
System.out.println(NiceEncoder.encode(first, second));
}
}
I get the java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
error.
Why? How do I fix it other than importing the commons-codec
inside the project with the TestNiceEncoder
class? How do I make it so everyone who has the base64tester.jar
can run it with no errors or additional actions?
EDIT: It's the same thing with other dependencies in base64tester
project. Got it to use JSoup
library and then if the method using it gets called from outside project, there's exception:
In project B:
public class TestNiceEncoder {
public static void main(String[] args) {
String first = "some word";
String second = "other word";
// System.out.println(NiceEncoder.encode(first, second));
System.out.println(NiceEncoder.getRedditTitle()); //jsoup error here
}
}
In project A (base64tester):
public class NiceEncoder {
public static String encode(String first, String second) {
String encoded = new String(Base64.encodeBase64((first + ":" + second).getBytes()));
return encoded;
}
public static String getRedditTitle() {
try {
Document doc = Jsoup.connect("http://reddit.com/").get();
String title = doc.title();
System.out.println("title: " + title);
return title;
}
catch (Exception e) {
System.out.println("Couldn't open URL");
return "";
}
}
}
<dependencies>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
</dependencies>
error is this one:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
答案1
得分: 4
这个步骤
> 然后将它打包成一个jar文件并安装到本地Maven仓库中
需要你指定pom.xml文件,否则会生成一个没有依赖信息的文件。
类似这样(当然,你需要根据你的pom.xml文件路径进行调整)
mvn install:install-file -Dfile=/home/base64tester-1.0-SNAPSHOT.jar -DpomFile=/home/pom.xml
更多信息请参阅官方文档。
或者,你可以在你的下游项目中显式声明对commons-codec的依赖(在那里你使用你的jar文件)。
另外,JDK8已经包含了对base64编解码的支持,请参见这里 - 所以也许你可以直接使用它,而不需要额外添加类路径上的任何jar文件。
英文:
This step
> Then I package it into a jar and install it in local maven repository
require you to specify pom.xml file, otherwise one is generated, but without dependency info
Something like (of course you need to adjust path to pom.xml)
mvn install:install-file -Dfile=/home/base64tester-1.0-SNAPSHOT.jar -DpomFile=/home/pom.xml
read more in official docs.
Alternatively you can declare explicit dependency on commons-codec in your downstream project (where you're consuming your jar)
Side note, JDK8 already includes support for base64 codec, see here - so maybe you can use it directly without any extra jars on classpath
答案2
得分: 0
Base64Tester的依赖包未包含在jar文件中。您应该从Base64Test项目中使用mvn instal。
英文:
The dependencies package of Base64Tester are not included in jar file. You should use mvn instal from Base64Test project
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论