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