I am trying to load dependencies on a Ubuntu 20.04 Machine using the java -cp
option.
Expected behavior:
Add GSON to the classpath, before starting, so i don't need to shade big dependencies to my Application.
I use the following Test Setup:
- a jar file in the directory /opt/api called dependtesting.jar
- a "lib" folder in the same directory
- the gson-2.8.6.jar in the lib folder.
The program itself:
package dev.epeu.depend;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public final class Test {
public static void main(String[] options) {
System.out.println("running depend test...");
Gson gson = new GsonBuilder().serializeNulls().setPrettyPrinting().create();
System.out.println(gson.toJson("Test in JSON representation"));
}
}
I build the software using gradle and shadow-jar.
build.grade:
plugins {
id 'com.github.johnrengelman.shadow' version '6.0.0'
}
jar {
manifest {
attributes 'Implementation-Title': 'Depend Testing',
'Implementation-Version': '1.0.0',
'Main-Class': 'dev.epeu.depend.Test'
}
}
allprojects {
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs += '--enable-preview'
}
apply plugin: 'java-library'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'com.github.johnrengelman.shadow'
group 'dev.epeu'
version '1.0.0'
sourceCompatibility = '14'
targetCompatibility = '14'
repositories {
mavenCentral()
}
dependencies {
compileOnly group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
}
}
I run the software using the following command:
java --enable-preview -cp "lib/*:." -jar dependtesting-1.0.0-all.jar
Result:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder
at dev.epeu.depend.Test.main(Test.java:10)
Caused by: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
Any idea how to deal with this? I have really bad internet and I really cannot upload 20mb of files at every test.