UFO ET IT

Gradle-기본 클래스를 찾거나로드 할 수 없습니다.

ufoet 2020. 12. 7. 21:17
반응형

Gradle-기본 클래스를 찾거나로드 할 수 없습니다.


Gradle을 사용하여 매우 간단한 프로젝트를 실행하고 다음을 사용할 때 다음 오류가 발생합니다 gradlew run command.

could not find or load main class 'hello.HelloWorld'

내 파일 구조는 다음과 같습니다.

SpringTest
    -src
        -hello
            -HelloWorld.java
            -Greeter.java
    -build
         -libs
         -tmp
    -gradle
         -wrapper
    -build.gradle
    -gradlew
    -gradlew.bat

libs 및 tmp 폴더의 내용은이 문제와 관련이있을 것이라고 생각하지 않았기 때문에 제외했지만 필요한 경우 추가 할 수 있습니다.

다음은 내 build.gradle 파일입니다.

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'

mainClassName = 'hello/HelloWorld'

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile "joda-time:joda-time:2.2"
}

jar {
    baseName = "gs-gradle"
    version = "0.1.0"
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

이 문제를 해결하는 방법에 대한 아이디어가 있습니까? mainClassName 속성에 대해 모든 종류의 작업을 시도했지만 아무것도 작동하지 않는 것 같습니다.


여기 두 가지 문제, 하나의 참조 sourceSet와 다른 mainClassName.

  1. 에 어느 이동 자바 소스 파일 src/main/java대신의 src. 또는 sourceSetbuild.gradle에 다음을 추가하여 올바르게 설정 하십시오.

    sourceSets.main.java.srcDirs = ['src']
    
  2. mainClassName 경로가 아닌 정규화 된 클래스 이름이어야합니다.

    mainClassName = "hello.HelloWorld"
    

build.gradle을 수정하여 메인 클래스를 매니페스트에 넣습니다.

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version,
                   'Main-Class': 'hello.helloWorld'
    }
}

나는 방금이 문제에 부딪 쳤고 인터넷에서 해결책을 찾을 수 없기 때문에 직접 디버깅하기로 결정했습니다. 내가 한 것은 mainClassName을 전체 경로로 변경하는 것입니다 (프로젝트 ofc의 올바른 하위 디렉토리 포함).

    mainClassName = 'main.java.hello.HelloWorld'

게시물이 작성된 지 거의 1 년이 지났지 만 누군가이 정보가 유용하다고 생각합니다.

즐거운 코딩입니다.


그냥 넷빈즈에서 Gradle을 프로젝트를 실행하는 초보자를위한 명확하게 설명하기 :
이 이해하기를, 당신은 무엇과 같은 기본 클래스 이름 모습 볼 필요 같은 어떤 Gradle을 빌드 외모 :

메인 클래스 :

package com.stormtrident;

public class StormTrident {
    public static void main(String[] cmdArgs) {
    }
}

"com.stormtrident"패키지의 일부입니다.

Gradle 빌드 :

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'
}

Struggled with the same problem for some time. But after creating the directory structure src/main/java and putting the source(from the top of the package), it worked as expected.

The tutorial I tried with. After you execute gradle build, you will have to be able to find classes under build directory.


In my build.gradle, I resolved this issue by creating a task and then specifying the "mainClassName" as follows:

task(runSimpleXYZProgram, group: 'algorithms', description: 'Description of what your program does', dependsOn: 'classes', type: JavaExec) {
    mainClassName = 'your.entire.package.classContainingYourMainMethod'
}

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);
}

I fixed this by running a clean of by gradle build (or delete the gradle build folder mannually)

This occurs if you move the main class to a new package and the old main class is still referenced in the claspath


When I had this error, it was because I didn't have my class in a package. Put your HelloWorld.java file in a "package" folder. You may have to create a new package folder:

Right click on the hello folder and select "New" > "Package". Then give it a name (e.g: com.example) and move your HelloWorld.java class into the package.


  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

enter image description here


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

For a project structure like

project_name/src/main/java/Main_File.class

in the file build.gradle, add the following line

mainClassName = 'Main_File'

참고URL : https://stackoverflow.com/questions/24924932/gradle-could-not-find-or-load-main-class

반응형