S
S
Stepan Kovalev2020-04-18 01:23:29
Java
Stepan Kovalev, 2020-04-18 01:23:29

How to consider dependencies when building a jar file?

There is some java project with build.gradle like this:

plugins {
    id 'java'
    id 'application'
    id 'com.github.johnrengelman.shadow' version '2.0.1'
}
mainClassName = "Main"
group 'org.q'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'net.dv8tion', name: 'JDA', version: '4.1.1_121'
    compile group: 'org.mongodb', name: 'mongodb-driver-sync', version: '3.12.2'
    compile group: 'org.atteo.classindex', name: 'classindex', version: '3.4'
    annotationProcessor group: 'org.atteo.classindex', name: 'classindex', version: '3.4'
}

jar {
    manifest {
        attributes 'Main-Class': 'discordbot.Main'
    }
}

compileJava.options.encoding = 'UTF-8'


When you try to build the project in a jar by calling "gradle jar", the desired file is successfully created, but when you try to execute it (java -jar "path/file.jar"), the following comes out:
Exception in thread "main" java.lang.NoClassDefFoundError: net/dv8tion/jda/api/JDABuilder
        at discordbot.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: net.dv8tion.jda.api.JDABuilder
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        ... 1 more

It is logical to assume that the file is built without dependencies, hence the question: how to configure gradle so that it either stuffs all dependent files into a jar, or puts them side by side in the form of jars and prescribes class paths?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-04-18
@xez

Seems like something is missing.

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'

https://imperceptiblethoughts.com/shadow/getting-s...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question