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

huangapple go评论127阅读模式
英文:

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 + &quot;:&quot; + second).getBytes()));
        return encoded;
    }
}

It has this pom.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;base64tester&lt;/groupId&gt;
    &lt;artifactId&gt;base64tester&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;

    &lt;properties&gt;
        &lt;maven.compiler.target&gt;11&lt;/maven.compiler.target&gt;
        &lt;maven.compiler.source&gt;11&lt;/maven.compiler.source&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;commons-codec&lt;/groupId&gt;
            &lt;artifactId&gt;commons-codec&lt;/artifactId&gt;
            &lt;version&gt;1.6&lt;/version&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;
&lt;/project&gt;

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:

&lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;base64tester&lt;/groupId&gt;
            &lt;artifactId&gt;base64tester&lt;/artifactId&gt;
            &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;

and try running this code:

public class TestNiceEncoder  {
   public static void main(String[] args) {
        String first = &quot;some word&quot;;
        String second = &quot;other word&quot;;
        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 = &quot;some word&quot;;
        String second = &quot;other word&quot;;
       // 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 + &quot;:&quot; + second).getBytes()));
        return encoded;
    }

    public static String getRedditTitle()  {
        try {
            Document doc = Jsoup.connect(&quot;http://reddit.com/&quot;).get();
            String title = doc.title();
            System.out.println(&quot;title: &quot; + title);
            return title;
        }
        catch (Exception e)  {
            System.out.println(&quot;Couldn&#39;t open URL&quot;);
            return &quot;&quot;;
        }
    }
}
 &lt;dependencies&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;commons-codec&lt;/groupId&gt;
            &lt;artifactId&gt;commons-codec&lt;/artifactId&gt;
            &lt;version&gt;1.6&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;org.jsoup&lt;/groupId&gt;
            &lt;artifactId&gt;jsoup&lt;/artifactId&gt;
            &lt;version&gt;1.13.1&lt;/version&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;

error is this one:

Exception in thread &quot;main&quot; 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

huangapple
  • 本文由 发表于 2020年10月15日 06:44:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64362488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定