Reputation: 3
I have a java console app with 5 classes and has one dependancy, gradle, com.google.code.gson.
I do build an executable jar successfully, however I get the following error when I execute the jar using the command java -jar <jarName>.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson 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)
Creating the artifact:
I expected the jar file to run
Upvotes: 0
Views: 128
Reputation: 2979
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
test {
useJUnitPlatform()
}
jar {
manifest {
attributes "Main-Class": "org.example.Main"
}
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
IDEA Gui Right Side : Gralde Tab , YourProject -> Tasks
-> jar
Find output jar file : YourProject -> build/libs : yourApp.jar
java -jar yourApp.jar
This method is create a fat jar, include dependencies class, only one jar.
No need other external jar files.
Upvotes: 0