Gradle Jar It hasn't found the main class

Hi guys.

I’m getting a little trouble when I want to run my application, once I have created my Jar, The error that I’m getting is like I have written above in the topic.

Here, I show you my root project:

±-- Project ‘:lab-myapp-api’
±-- Project ‘:lab-myapp-commons’
±-- Project ‘:lab-myapp-config’
±-- Project ‘:lab-myapp-model’
±-- Project ‘:lab-myapp-service’
— Project ‘:lab-myapp-starter’

Where sub project lab-myapp-starter is my spring boot context and this is I want to run. When I run the application from my IDE IntelliJ the applicatio. works and process all requests, but When I have created my jar and It tries to run the application with Jar I haven’t could.

This is my gradle build file of the project.

subprojects{
apply plugin: ‘java’

sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()
}

dependencies {
    annotationProcessor 'org.projectlombok:lombok:1.18.10'
    compileOnly 'org.projectlombok:lombok:1.18.10'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

jar {
    manifest {
        attributes(
                'Main-Class': 'com.mycompany.lab.myapp.starter.MyApplication'
        )
    }

    doFirst {
        manifest {
            if (!configurations.runtime.isEmpty()) {
                attributes('Class-Path':
                        configurations.runtime.collect{it.toURI().toString()}.join(' '))
            }
        }
    }
}

}

group ‘com.mycompany’
version ‘1.0-SNAPSHOT’

Any idea or solution about that?

Thanks a lot for you help.

Hi @ggeliso,

I am not sure what the concrete problem is, but here are some pointers that should help you:

  • Consider using the application instead of the plain java plugin. Then Gradle will do all the work of creating the manifest and packaging the application for you - https://docs.gradle.org/current/userguide/application_plugin.html
  • If you absolutely need to do something manually, you should use configurations.runtimeClasspath instead of configurations.runtime (runtime is deprecated and might not always be complete).

Jendrik thanks for you answer.

Before, I want to be completely clear in my question, for that I say you the next:

My main issue is that I haven’t could to run my application from command line after of I have created an executable JVM application, the error that I’m getting is “It hasn’t been found or loaded the main class”.

I don’t know that I have something wrong in my gradle configuration, is for this reason I showed you the gradle build, in order to, you can see any error and you can say me any solution or give me any guide about my issue.

In the other hand, If my problem could be solving with your suggest, Could you give me a little more information or show me an example about how I can to implement the plugin application?

again, Thanks a lot.

Did you read the documentation on the application plugin I linked?
https://docs.gradle.org/current/userguide/application_plugin.html

You can do this for example:

in lab-myapp-starter/build.gradle

plugins {
    id "application"
}
application {
    mainClassName = "com.mycompany.lab.myapp.starter.MyApplication"
}

And then you can run your application with:

./gradlew :lab-myapp-starter:run

Or get a fully packaged app with:

./gradlew :lab-myapp-starter:distZip

Thanks Jendrik for you help.

1 Like