Creating .jar with gradle causes a java.lang.NoClassDefFoundError - java

I use vscode and gradle to build my java jar. In the project I import com.google.gson.Gson and can create an object of Gson. If I run the code everything works fine, but if I use gradlew build to create the jar and when I try to executed the jar file then I get this Error: "Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/GsonBuilder"
dependencies {
// Use JUnit Jupiter for testing.
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'
implementation 'com.google.code.gson:gson:2.10'
}
jar {
manifest {
attributes(
'Main-Class': 'analyser.Run'
)
} }
I do not understand what is wrong here. Any ideas?

I had a mistake in the build.gradle, now it works by using
plugins {
id 'java'
id 'application'
id 'distribution'
}

Related

How to remove "extras" from using java gradle shadow plugin

I'm using the gradle shadow plugin in my java project.
plugins {
id 'java'
id 'com.github.johnrengelman.shadow' version '7.0.0'
}
I used the following code to get rid of all the dependencies I could.
shadowJar {
dependencies {
exclude(dependency("org.spigotmc:spigot-api:1.16.5-R0.1-SNAPSHOT"))
exclude(dependency("org.jetbrains:annotations:22.0.0"))
exclude(dependency("com.comphenix.protocol:ProtocolLib:4.7.0"))
}
}
However, I've got all these extra files in my jar file. I want to remove everything except what's in the "eu" folder and the "plugin.yml". How can I do this?
Don't use shadowJar {} unless you have to, just change "implementation" to "compileOnly" for the dependencies you don't want in the jar

How do you create then run an executable jar with dependencies in Intellij IDEs, specifically Android Studio?

I'm trying to create and run an executable jar through gradle. This is what my current gradle looks like:
task jarTask(type: Jar) {
baseName = 'my-main-class'
from 'build/classes/main'
}
task createJarWithDependencies(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Sample Jar',
'Implementation-Version': 1,
'Main-Class':'com.example.MyMainClass'
}
baseName = "my-main-class-with-dependencies"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('build/libs/my-main-class-with-dependencies.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
runMyJar.dependsOn(createJarWithDependencies, build)
I got these approach from the following stack overflow answers/references below:
Android Studio export jar with dependencies
Android studio - How to use an executable jar file from gradle
However when I run ./gradlew clean runMyJar (or even just ./gradlew runMyJar, I get the following error message:
Error: Could not find or load main class com.example.MyMainClass
Can anyone point out the reason why my executable jar is not finding the main method inside my class? Is there anything I'm missing?
Since I haven't had much luck with this solution, I managed to get around this problem by doing the following instead:
task deleteJar(type: Delete) {
delete 'libs/my-main-class.jar'
}
task createJar(type: Copy) {
from('build/intermediates/bundles/debug/')
into('libs/')
include('classes.jar')
rename('classes.jar', 'my-main-class.jar')
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('libs/my-main-class.jar')
classpath files('libs/common-util.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
createJar.dependsOn(deleteJar, build)
runMyJar.dependsOn(createJar, build)
This is not the solution that I want though, since this approach doesn't pull in the dependencies from other modules referenced by this module, which is what I was trying to solve. In order to do that, what I end up doing is copy/paste this same approach on those "other" modules and generate a lib for that module, and then copy that lib over to the libs folder of this module. OR I can move those dependent code in this same module so I don't have to deal with it. For a simple application that's not too bad (which, luckily, is my situation), but for a complex one, I'm not sure how to go about that.
As an added reference here is my dependencies section:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':common-util')
// common-util is an Android module, which references other Android
// methods such as Log.d, android.util class methods, etc.
}
By the way, I'm using the Android Studio IntelliJ IDE for this (which was the original motivation of my post). My hope was to not have to create separate jars from other modules to include it to this module but instead do it all in one go ...

Build is successful but the runtime dependency seems bad in Gradle dependency

dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile group: 'org.apache.commons', name: 'commons-math3' , version: '+'
testCompile 'junit:junit:4.12'
}
Even if I add this, when I run gradle build, it works, and codes with commons-math3 can be compiled. But when I run a jar file in build/,
it says Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/complex/Complex
But the official Gradle site says, the resource in 'compile' will also be included in 'runtime' dependency. and I also tried adding the commons-math to runtime. but it does not work.
Maybe this is my misunderstood of the dependency system.
How can I include external library from maven repository into a jar file made by the Gradle.
What you are looking for is either the distribution zips produced by the application plugin or the shadow jar (also called fat jar) produced by the shadowJar plugin:
The distribution zip (application plugin)
About the distribution zip
The distribution zips look like this:
my-app-0.2.0.zip
├──bin
│ ├──my-app
│ └──my-app.bat
└──lib
├──my-app-0.2.0.jar
├──slf4j-api.1.7.13.jar
└──commons-math3-3.6.jar
You can then run your application with its dependencies by unzipping what has been produced in build/distributions/ and running either my-app.bat (on windows) or ./my-app (on linux or OS X)
Building a distribution zip
Here is a sample gradle build file for making a distribution zip:
build.gradle
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'org.myapp.Main'
repositories { jcenter() }
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'org.apache.commons:commons-math3:3.6'
testCompile 'junit:junit:4.12'
}
Can be run with gradle distributionZip or gradle distributionTar. To just run the application, use gradle run.
The shadow jar
About the shadow jar
The shadow jar is one giant jar file that is a combination of your program and its libraries, packed together into one file. You will get a file that is self-contained and can be run by a double-click on most systems (e.g. on Windows that works, on Xubuntu it can be run by right-clicking and selecting 'Run with Oracle Java 8 Runtime', etc...).
Building a distribution zip
Here is, again, a sample build.gradle file:
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
mainClassName = 'org.myapp.Main'
jar {
manifest {
attributes('Main-Class': mainClassName)
}
}
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
}
}
repositories { jcenter() }
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13'
compile 'org.apache.commons:commons-math3:3.6'
testCompile 'junit:junit:4.12'
}
Run it with gradle shadowJar - Your jar with packed dependencies will be in build/libs and it will be named my-app-x.x.x-all.jar.
Gradle is first of all a build tool (just like maven, btw).
Its "responisiblity" starts when you feed it a source file and ends when you get your artifact (in your case its a jar).
Now when you're going to actually run your application there is a plethora of different options here.
If you just run java -jar <your_jar> you are responsible by yourself to construct the classpath.
If you run it with some kind of external runner, you should read the documentation of it and supply it a classpath.
Hope this helps

intellij build jar artifact containing gradle dependencies

I basically want to do something simple - or atleast i think it should be pretty simple.
My goal is to create an Intellij gradle project, add some dependencies to the module using gradle and add some java source code to it.
Then I just want to have an option to somehow compile the whole thing into 1 jar, containing all grade dependencies and being able to execute using "java -jar"
However it turned out that this is not as easy is i had thought.
I just created a new gradle project from intellij and added a Main class.
I´ll give you an overview over my files:
settings.gradle:
rootProject.name = 'gradleTestNewJar'
build.gradle:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
sourceCompatibility = 1.6
version = '1.0'
repositories {
mavenCentral()
}
mainClassName = "com.randomPackage.StarterClass"
dependencies {
compile 'org.seleniumhq.selenium:selenium-java:2.46.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
main class:
package com.randomPackage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class StarterClass {
public static void main(String[] args){
System.out.println("test");
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
driver.quit();
}
}
The main method of "MyStart" is executed when running from Intellij via debug.
So it works, when all dependencies get loaded correctly.
NOTE: I use Intellij Community Edition if this makes any difference.
What i tried:
1. I tried to just use "gradlew clean build".
This created a jar, but without libs.
But I didn´t expect it to be as easy as this.
2. I tried to build an artifact of the module as suggested here:
http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
I tried it with extracted and not extracted dependencies.
In both cases the dependencies were added into the jar, but they were added to the root of the jar.
When i tried to run the jar file via "java -jar", it complained:
"Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
..."
OK, so it couldn´t load the dependencies.
NOTE: I thought that the dependencies were not added to the classpath, but i am not sure about this. However, i would expect Intellij to add dependencies to the classpath( or declare in the manifest file)
3. I also tried to use the gradle application plugin.
However this creates a zip/tar which contains a execute script and a bin folder which was not my intention.
So i started googling for hours and hours but i cann´t find a solution to my problem.
Come on this cannot be so hard - it is just so basic.
I am sure some genius can help me out and point me to my - probably stupid - failure.
My current solution is as follows:
I use gradle to build a jar containing all libs, I do this witha custom task called fatJar.
Here is a part from my build.gradle
apply plugin: 'java'
jar {
manifest {
attributes("Manifest-Version": "1.0",
"Main-Class": "com.randomPackage.MainClass");
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
Then I just execute "gradle fatJar" on the command line and get a perfect jar.

Gradle Dependencies aren't included in jar

I'm very new to Gradle and the main reason I use it because I need to resolve dependencies issues for my compiled jar.
Here is my build.gradle file:
apply plugin: 'java'
apply plugin: 'eclipse'
version = '0.9'
jar {
manifest {
attributes 'Main-Class': 'wd.nlp.FileImport', 'Implementation-Title': 'WD\
NLP', 'Implementation-Version': version
}
}
repositories {
flatDir(dirs: 'lib')
}
dependencies{
compile 'mallet:mallet:2.0.7'
}
My Java files all depend on a third-party library called Mallet. Here is my folder structure:
|---1.10
|-----taskArtifacts
|-build
|---classes
|-----main
|-------wd
|---------nlp
|---dependency-cache
|---libs
|---tmp
|-----jar
|-lib
|-src
|---main
|-----java
|-------wd
|---------nlp
|-----resources
I placed Mallet-2.0.7.jar inside lib under the top folder. Then I used gradle build command and try to run my class:
java -classpath build/libs/JavaClassifier-0.9.jar wd.nlp.FileImport -train ...
Then I am told:
Exception in thread "main" java.lang.NoClassDefFoundError: cc/mallet/pipe/Pipe
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2531)
The Mallet library's path is cc.mallet.*...and now I don't know what to do...please help!
The dependency is not include because you don't create a distribution of you project. In Maven it is called an assembly.
If you call your application directly from the commandline it is not a matter of Gradle.
Anyway there is a Gradle plugin which creates a distribution and adds shell scripts to execute your application.
So when you add the following line to the gradle script.
apply plugin: 'application'
you will get some new tasks to create a distribution.

Categories