0

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.

Jan Bredow
  • 31
  • 3
  • Open the jarfile with your favorite zip utility. If gson isn't in there, your shadow configuration for gradle didn't work. – Rogue Oct 23 '20 at 07:01
  • thats not what i am trying to do. pls read the Question completely – Jan Bredow Oct 23 '20 at 07:02
  • The command is `java`. The `-cp` part is an option. – user207421 Oct 23 '20 at 07:08
  • @MarquisofLorne that's correct. But that's not the question. – Jan Bredow Oct 23 '20 at 07:09
  • 1
    Java 14 is out of support, you should upgrade to Java 15. – Mark Rotteveel Oct 23 '20 at 07:10
  • Unable to update to Java 15. Software is written in Java 14. – Jan Bredow Oct 23 '20 at 07:12
  • @JanBredow The question needs to be expressed in terminology that is mutually understood, and it wasn't. – user207421 Oct 23 '20 at 07:13
  • Sorry, my english kinna sucks, i'm from germany. – Jan Bredow Oct 23 '20 at 07:14
  • 1
    Using `-jar` means `-cp` is ignored. That's always been the case and isn't new. – Joachim Sauer Oct 23 '20 at 07:24
  • @JanBredow Following up, _something_ needs to load the files from GSON. Whether that's you specifying a `ClassLoader`, or specifying it via `-cp` _correctly_. So to do that, you would actually reference all of the jarfiles you want to use, and then the main class to start: `java -cp gson-2.8.6.jar;dependtesting-1.0.0-all.jar dev.epeu.depend.Test`, which is `java -cp ; `. Keep in mind this shouldn't be a production/live code trick, as managing across different gson versions will break – Rogue Oct 23 '20 at 07:33

0 Answers0