简体   繁体   中英

Could not find or load main class, after build by gradle

I've made a project in maven and spring boot. After build it via maven it worked perfect. After all I decided to swap my project into gradle. And now, after:

gradle build

The following exception is comming.Error:

Error: Could not find or load main class

Here are things I checked before I asked this question:

  • made sure that I have main method (which i obviously have, maven did thing great)
  • checked path to main class in manifest and in jar task in gradle
  • found out that compiled class is in specified jar in specified path
  • Made a jar task in gradle that looks like this:

     jar { manifest { attributes 'Main-Class': 'pl.sygnity.schedulein.App' } } 

I have no idea what i can do more about it. Could you help me?

Edit. It's important i wish to use my program as jar so:

java -jar xx.jar

Edit2.

gradle run

makes my App start. So it looks like as if gradle build is not working somehow...

You need to modify your gradle manifest.

jar {
    manifest {
        attributes <...>
                   'Main-Class': 'main.myMainClass'
    }
}

Otherwise you can change the path of your source fields like this.

sourceSets.main.java.srcDirs = ['MAINFOLDER'] // 'src'

You need to define the main class in the build.gradle file. (You may have more than one, and you need to choose which one to use)

I like to do it with Gradle - the application plugin

apply plugin: 'application'
mainClassName = "<Your main class>"

Then you can run gradle install to build the program with an executor.

So i find out that these steps made my jar working well.

Removed

apply plugin: 'maven'

Added

apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

Bit weird but maybe someone will be struggling with same kind of problem. Thanks for help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM