Gradle - Could not find or load main class

前端 未结 13 772
星月不相逢
星月不相逢 2020-11-29 06:51

I\'m trying to run a very simple project using Gradle and running into the following error when using the gradlew run command:

could not find or l

相关标签:
13条回答
  • 2020-11-29 07:37

    I just ran into this problem and decided to debug it myself since i couldn't find a solution on the internet. All i did is change the mainClassName to it's whole path(with the correct subdirectories in the project ofc)

        mainClassName = 'main.java.hello.HelloWorld'
    

    I know it's been almost one year since the post has been made, but i think someone will find this information useful.

    Happy coding.

    0 讨论(0)
  • 2020-11-29 07:37

    For Netbeans 11 users, this works for me:

    apply plugin: 'java'
    apply plugin: 'application'
    
    // This comes out to package + '.' + mainClassName
    mainClassName = 'com.hello.JavaApplication1'
    

    Here generally is my tree:

    C:\...\NETBEANSPROJECTS\JAVAAPPLICATION1
    │   build.gradle
    ├───src
    │   ├───main
    │   │   └───java
    │   │       └───com
    │   │           └───hello
    │   │                   JavaApplication1.java
    │   │
    │   └───test
    │       └───java
    └───test
    
    0 讨论(0)
  • 2020-11-29 07:39
    1. verify if gradle.properties define right one JAVA_HOVE

      org.gradle.java.home=C:\Program Files (x86)\Java\jdk1.8.0_181

    or

    1. if it's not defined be sure if Eclipse know JDK and not JRE

    0 讨论(0)
  • 2020-11-29 07:45

    I resolved it by adding below code to my application.

    // enter code here this is error I was getting when I run build.gradle. main class name has not been configured and it could not be resolved

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    0 讨论(0)
  • 2020-11-29 07:47

    If you decided to write your hello.World class in Kotlin, another issue might be that you have to reference it as mainClassName = "hello.WorldKt".

    src/main/java/hello/World.kt:

    package hello
    fun main(args: Array<String>) {
        ...
    }
    // class World {} // this line is not necessary
    
    0 讨论(0)
  • 2020-11-29 07:51

    Just to make it clear for newbies trying to run a gradle project from Netbeans:
    To understand this, you need to see what the main class name looks like and what the gradle build looks like:

    Main class:

    package com.stormtrident;
    
    public class StormTrident {
        public static void main(String[] cmdArgs) {
        }
    }
    

    Notice that it is part of the package "com.stormtrident".

    Gradle build:

    apply plugin: 'java'
    
    defaultTasks 'jar'
    
    jar {
     from {
            (configurations.runtime).collect {
                it.isDirectory() ? it : zipTree(it)
            }
        }    
        manifest {
            attributes 'Main-Class': 'com.stormtrident.StormTrident'
        }
    }
    
    
    sourceCompatibility = '1.8'
    [compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
    
    if (!hasProperty('mainClass')) {
        ext.mainClass = 'com.stormtrident.StormTrident'
    }
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        //---apache storm
        compile 'org.apache.storm:storm-core:1.0.0'  //compile 
        testCompile group: 'junit', name: 'junit', version: '4.10'
    }
    
    0 讨论(0)
提交回复
热议问题