简体   繁体   中英

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

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

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

The dependencies package of Base64Tester are not included in jar file. You should use mvn instal from Base64Test project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM