java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64: from a library jar imported with Maven

java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64: from a library jar imported with Maven

我制作了一个简单的库 jar,其中包含这个 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;
    }
}

它有这个 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 class 在项目中导入 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 error here
    }
}

在项目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>

错误是这个:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup

Base64Tester的依赖包没有包含在jar文件中。您应该使用 Base64Test 项目中的 mvn instal

这一步

Then I package it into a jar and install it in local maven repository

要求你指定pom.xml文件,否则会生成一个,但没有依赖信息

类似的东西(当然你需要调整路径到pom.xml)

mvn install:install-file -Dfile=/home/base64tester-1.0-SNAPSHOT.jar -DpomFile=/home/pom.xml

official docs 阅读更多内容。

或者,您可以在下游项目(您使用 jar 的地方)commons-codec 中声明对 commons-codec 的显式依赖

旁注,JDK8 已经包含对 base64 编解码器的支持,see here - 因此也许您可以直接使用它而无需在类路径上添加任何额外的 jar