I am building a jar file using gradle. I have local jar files as dependencies. Those jars have additional local dependencies stored in the same directory. I used the following build.gradle file
plugins { id 'application' }
repositories {
flatDir{ dirs 'lib'
}
dependencies {
implementation name: 'localjar1'
implementation name: 'localjar2'
}
application { mainClass = 'mypackage.MyApp' }
jar {
from { configurations.runtimeClasspath.collect {it.isDirectory ? : zipTree(it) } }
manifest { attributes 'Main-Class': 'mypackage.MyApp' }
}
I built the jar using gradle jar, but when I try to run the jar:
java -jar myapp\build\libs\myapp.jar
I get the following error:
Unable to initialize main class mypackage.MyApp
Caused by java.lang.NoClassDefFoundError: com/localpackage/SomeClass
How do bring that dependency in correctly?
I also tried to include the entire local library as a dependency
implementation fileTree(dir: 'lib', include: '*jar')
But when I do this I get the error: java.lang.ClassNotFoundException: mypackage.MyApp
What else should I try?
** Note: I can run the application using gradle run
I'm using Intellij Idea, with gradlew version 6.8.
Whenever I point implementation keyword for dependencies in build.gradle - I get(example with gson dependency) this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at com.epam.adnocove.search.job.EmailVerifier.<clinit>(EmailVerifier.java:19)
at com.epam.adnocove.search.job.UserIndexPipeline.<clinit>(UserIndexPipeline.java:31)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:636)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:182)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:519)
... 2 more
my build.gradle file:
plugins {
id 'java'
}
group 'com.epam.adnocove'
version '1.0'
repositories {
mavenCentral()
}
jar {
manifest {
attributes 'Main-Class': 'Program'
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
dependencies {
implementation 'com.google.code.gson:gson:2.8.2'
testImplementation 'junit:junit:4.12'
}
If I put compile instead of implementation - everything works as expected. But what if I don't want transitive dependency. Why gradle doesn't put my dependency to my jar?
The problem is that by default build along with java plugin only - Gradle doesn't pack dependencies into jar - therefore we get an exception.
The solution:
we can use fat jars(by separate task), or use java application plugin which will create OS-specific startup applications which can run built jar.
new to gradle and java here and I am trying to use Android's Log method from android.util.Log. It seems I can compile and it finds what it needs, but it can not find it at runtime. I have tried using 'runtime' instead of 'compile' in the dependencies section, but not luck.
java -jar build/libs/testJavaHttp.jar
Exception in thread "main" java.lang.NoClassDefFoundError: android/util/Log
at myproject.test.HttpToFile.downloadFile(HttpToFile.java:20)
at myproject.test.Main.main(Main.java:12)
Caused by: java.lang.ClassNotFoundException: android.util.Log
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
In build.gradle
apply plugin: 'java'
dependencies {
compile files('../androidsdk/platforms/android-25/android.jar')
}
jar {
manifest {
attributes 'Main-Class': 'myproject.test.Main'
}
}
You are trying to run the jar and the jar is not packed with dependencies, use shadow jar plugin or pack required artifacts in, by extending the jar task.
jar {
archiveName = 'Name.jar'
manifest {
attributes 'Main-Class': 'myproject.test.Main',
'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '),
'Implementation-Version': 1.0
}
from(configurations.myconfig.collect { it.isDirectory() ? it : zipTree(it) }) {
// in here you can exclude what you need as well if needed.
}
}
To not pull the 'entire world' into a jar you can create configuration just with libraries you require:
configurations{
myconfig // to create configuration
compile.extendsFrom(myConfig) //to include it in compile as well
}
and then use this config in the jar creation and in dependecies.
dependencies {
myconfig files('../androidsdk/platforms/android-25/android.jar')
}
But looking at your code there is not much to pull there. You need to consider packing all needed artifacts and transitives if you expect it to be a runnable jar, as you run it with 'java -jar [...]' command.
PS. Everything that is in compile is going to be included in runtime configuration as well.
I am trying to run a jar from Android studio. After a long Workaround, the jar file run perfectly.
Now i need to export the jar file from the android studio. I got the Jar file from the build/libs folder.
But the problem is that the jar file shows a error.
no main manifest attribute, in "app.jar"
So i found this solution. Can't execute jar- file: "no main manifest attribute"
Then i read about MANIFEST.MF & added the mail class to that file.
jar {
manifest.attributes(
'Main-Class': "com.Remo.server.RemoServerApp"
)
}
After adding those in my gradle. My MANIFEST.MF contains the MainClass.
But im still getting the same error? How can i solve this ?
Note: The objective is that I want to Export a Runnable Jar file from the project.
UPDATE:
After Adding the MainClass in MANIFEST.MF. I got stuck with the below error.
Exception in thread "main" java.lang.NoClassDefFoundError: com/Remo/protocol/RemoConnection
at com.Remo.server.RemoServerApp.<init>(RemoServerApp.java:33)
at com.Remo.server.RemoServerApp.main(RemoServerApp.java:97)
Caused by: java.lang.ClassNotFoundException: com.Remo.protocol.RemoConnection
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.Remo.server.RemoServerApp
UPDATE 2
From your solution what i understood is that we need to copy the remoprotocol jar file to the remoserver.
remoserver project gradle file
apply plugin: 'java'
sourceSets {
main {
resources.srcDirs = ['src/main/resources']
}
}
dependencies {
compile project(':remoprotocol')
compile files('libs/bluecove-2.1.1.jar')
}
jar {
manifest.attributes(
'Main-Class': "com.remo.server.remoServerApp"
)
manifest.attributes(
'Class-Path': configurations.runtime.files.collect { it.getName() }.join(' '))
}
task copyRTDependenciesToLib(type: Copy) {
into "$buildDir/output/lib"
from configurations.runtime
}
After running the gradle task also i am getting the same error.
First you want to copy all your runtime dependencies into the builddir/libs folder (or a different distribution folder if you so choose). Here is a custom task that would achieve this:
task copyRTDependenciesToLib(type: Copy) {
into "$buildDir/libs"
from configurations.runtime
}
Add your runtime dependency jars as a Class-Path attribute in your manifest file. The jars need to be in the same directory as your runnable jar - which the copy task above achieves. (alternatively, you can provide full relative path for your dependency jar location)
jar {
manifest {
attributes(
"Main-Class": "com.Remo.server.RemoServerApp",
"Class-Path": configurations.runtime.files.collect { it.getName() }.join(' '))
)
}
}
Some more things to consider:
The application plugin does the same thing; it adds a task installDist that produces a runnable set of jars along with any dependencies, any readme's, documentation you want to include.
If you want to produce a single runnable jar without having to bundle dependencies along with it, you should look into creating a "fatjar", for example:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Gradle Jar File Example',
'Implementation-Version': version,
'Main-Class': "com.Remo.server.RemoServerApp"
}
baseName = project.name
//collect all dependencies
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
I have no experience with Android Studio (or Gradle), but I have with Java. Aren't you trying to set main class instead?
Therefore I suggest changing Class-Path attribute to Main-Class as the manifest should contain Main-Class to be able to invoke something when "running" JAR.
How to create Java Gradle project from command line?
It should create standard maven folder layout like on the picture below.
UPDATE:
.1. From http://www.gradle.org/docs/current/userguide/tutorial_java_projects.html
I need to create file build.gradle with 2 lines
apply plugin: 'java'
apply plugin: 'eclipse'
.2. Add to build.gradle task below, than execute gradle create-dirs
task "create-dirs" << {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
.3. Then run gradle eclipse (or corresponding string to other IDE plugin configured)
So is there way to do it in one command?
To create a Java project: create a new project directory, jump into it and execute
gradle init --type java-library
Source folders and a Gradle build file (including a wrapper) will be build.
The gradle guys are doing their best to solve all (y)our problems ;-).
They recently (since 1.9) added a new feature (incubating): the "build init" plugin.
See: build init plugin documentation
Finally after comparing all solution, I think starting from build.gradle file can be convenient.
Gradle distribution has samples folder with a lot of examples, and there is gradle init --type basic comand see Chapter 47. Build Init Plugin. But they all needs some editing.
You can use template below as well, then run gradle initSourceFolders eclipse
/*
* Nodeclipse/Enide build.gradle template for basic Java project
* https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/java/basic/build.gradle
* Initially asked on
* http://stackoverflow.com/questions/14017364/how-to-create-java-gradle-project
* Usage
* 1. create folder (or general Eclipse project) and put this file inside
* 2. run `gradle initSourceFolders eclipse` or `gradle initSourceFolders idea`
* #author Paul Verest;
* based on `gradle init --type basic`, that does not create source folders
*/
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
task initSourceFolders { // add << before { to prevent executing during configuration phase
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
// In this section you declare where to find the dependencies of your project
repositories {
// Use Maven Central for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
//compile fileTree(dir: 'libs', include: '*.jar')
// The production code uses the SLF4J logging API at compile time
//compile 'org.slf4j:slf4j-api:1.7.5'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile "junit:junit:4.11"
}
The result is like below.
That can be used without any Gradle plugin for Eclipse,
or with (Enide) Gradle for Eclipse, Jetty, Android alternative to Gradle Integration for Eclipse
Unfortunately you cannot do it in one command. There is an open issue for the very feature.
Currently you'll have to do it by hand. If you need to do it often, you can create a custom gradle plugin, or just prepare your own project skeleton and copy it when needed.
EDIT
The JIRA issue mentioned above has been resolved, as of May 1, 2013, and fixed in 1.7-rc-1. The documentation on the Build Init Plugin is available, although it indicates that this feature is still in the "incubating" lifecycle.
Here is what it worked for me.. I wanted to create a hello world java application with gradle with the following requirements.
The application has external jar dependencies
Create a runnable fat jar with all dependent classes copied to the jar
Create a runnable jar with all dependent libraries copied to a directory "dependencies" and add the classpath in the manifest.
Here is the solution :
Install the latest gradle ( check gradle --version . I used gradle 6.6.1)
Create a folder and open a terminal
Execute gradle init --type java-application
Add the required data in the command line
Import the project into an IDE (IntelliJ or Eclipse)
Edit the build.gradle file with the following tasks.
Runnable fat Jar
task fatJar(type: Jar) {
clean
println("Creating fat jar")
manifest {
attributes 'Main-Class': 'com.abc.gradle.hello.App'
}
archiveName "${runnableJar}"
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} with jar
println("Fat jar is created")
}
Copy Dependencies
task copyDepends(type: Copy) {
from configurations.default
into "${dependsDir}"
}
Create jar with classpath dependecies in manifest
task createJar(type: Jar) {
println("Cleaning...")
clean
manifest {
attributes('Main-Class': 'com.abc.gradle.hello.App',
'Class-Path': configurations.default.collect { 'dependencies/' +
it.getName() }.join(' ')
)
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
} with jar
println "${outputJar} created"
}
Here is the complete build.gradle
plugins {
id 'java'
id 'application'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.slf4j:slf4j-api:1.7.30'
implementation 'ch.qos.logback:logback-classic:1.2.3'
implementation 'ch.qos.logback:logback-core:1.2.3'
testImplementation 'junit:junit:4.13'
}
def outputJar = "${buildDir}/libs/${rootProject.name}.jar"
def dependsDir = "${buildDir}/libs/dependencies/"
def runnableJar = "${rootProject.name}_fat.jar";
//Create runnable fat jar
task fatJar(type: Jar) {
clean
println("Creating fat jar")
manifest {
attributes 'Main-Class': 'com.abc.gradle.hello.App'
}
archiveName "${runnableJar}"
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} with jar
println("Fat jar is created")
}
//Copy dependent libraries to directory.
task copyDepends(type: Copy) {
from configurations.default
into "${dependsDir}"
}
//Create runnable jar with dependencies
task createJar(type: Jar) {
println("Cleaning...")
clean
manifest {
attributes('Main-Class': 'com.abc.gradle.hello.App',
'Class-Path': configurations.default.collect { 'dependencies/' +
it.getName() }.join(' ')
)
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
} with jar
println "${outputJar} created"
}
Gradle build commands
Create fat jar : gradle fatJar
Copy dependencies : gradle copyDepends
Create runnable jar with dependencies : gradle createJar
More details can be read here : https://jafarmlp.medium.com/a-simple-java-project-with-gradle-2c323ae0e43d
If you are using Eclipse, for an existing project (which has a build.gradle file) you can simply type gradle eclipse which will create all the Eclipse files and folders for this project.
It takes care of all the dependencies for you and adds them to the project resource path in Eclipse as well.
I could handle it using a groovy method in build.gradle to create all source folders for java, resources and test. Then I set it to run before gradle eclipse task.
eclipseClasspath.doFirst {
initSourceFolders()
}
def initSourceFolders() {
sourceSets*.java.srcDirs*.each { it.mkdirs() }
sourceSets*.resources.srcDirs*.each { it.mkdirs() }
}
Now we can setup a new gradle Java EE project to eclipse with only one command. I put this example at GitHub
I just tried with with Eclipse Neon.1 and Gradle:
------------------------------------------------------------
Gradle 3.2.1
------------------------------------------------------------
Build time: 2016-11-22 15:19:54 UTC
Revision: 83b485b914fd4f335ad0e66af9d14aad458d2cc5
Groovy: 2.4.7
Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM: 1.8.0_112 (Oracle Corporation 25.112-b15)
OS: Windows 10 10.0 amd64
On windows 10 with Java Version:
C:\FDriveKambiz\repo\gradle-gen-project>java -version
java version "1.8.0_112"
Java(TM) SE Runtime Environment (build 1.8.0_112-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.112-b15, mixed mode)
And it failed miserably as you can see in Eclipse.
But sailed like a soaring eagle in Intellij...I dont know Intellij, and a huge fan of eclipse, but common dudes, this means NO ONE teste Neon.1 for the simplest of use cases...to import a gradle project.
That is not good enough.
I am switching to Intellij for gradle projects: