简体   繁体   中英

Gradle: "Error: Could not find or load main class Main". Can't run file after build via Gradle

Stack Overflow

Faced a problem while building java app using Gradle. After building the jar file, when trying to run it with

java -jar ZoneNewsDiscordBot-1.0-SNAPSHOT.jar

got this in terminal

Error: Could not find or load main class Main

My project file's hierarchy

- ...
- .idea
- build
    - classes
        - java
            - main
                - Main.class
                - NewsGenerator.class
                - Resources.class
    - libs
        - ZoneNewsDiscordBot-1.0-SNAPSHOT.jar
    - resources
        - main
            - META-INF
                MANIFEST.MF
            - application.properties
    - tmp
        - compileJava
        - jar 
            - MANIFEST.MF
- gradle
- img
- input
- out
- src
    - main
        - java
            - Main.java
            - NewsGenerator.java
            - Resources.java
- build.gradle
- gradlew
- gradlew.bat
- ...

build.gradle

plugins {
    id 'java'
}
 
jar {
    manifest {
        attributes(
                'Main-Class': "Main"
        )
    }
}
 
group 'com.ZoneNewsDiscordBot'
version '1.0-SNAPSHOT'
 
sourceCompatibility = 1.8
 
repositories {
    mavenCentral()
    jcenter()
}
 
dependencies {
    implementation "joda-time:joda-time:2.2"
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'net.dv8tion:JDA:4.2.0_214'
 
    //for JSON parsing
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.3'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.11.3'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.11.3'
 
 
    //for uploading pictures from Web Page
    compile group: 'net.sourceforge.htmlunit', name: 'htmlunit', version: '2.45.0'
 
    //for working with Instagram
    compile group: 'com.restfb', name: 'restfb', version: '3.13.0'
    implementation 'com.github.instagram4j:instagram4j:2.0.3'
    compile group: 'org.brunocvcunha.instagram4j', name: 'instagram4j', version: '1.14'
    
    task stage(dependsOn: ['build', 'clean'])
    build.mustRunAfter clean
}

I tried to check MANIFEST.MF and got this
MANIFEST.MF

Manifest-Version: 1.0
Main-Class: Main

I cannot understand what the problem is. I has written right paths... Why it cannot find Main class?

i solved it like this

This occurs because the class name including the package name is not written in the mainClassName entry of the build.gradle file.

EX)

1.

package example.sphinx;


public class ExampleMain {
  
}
plugin : 'java'
 
mainClassName = 'example.sphinx.ExampleClass'

2.Adding the application

plugins {
  id 'java'
  id 'application'
}

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