Gradle - Could not find or load main class

前端 未结 13 771
星月不相逢
星月不相逢 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:26

    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'
    
    0 讨论(0)
  • 2020-11-29 07:27

    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

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

    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.

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

    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.

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

    Modify build.gradle to put your main class in the manifest:

    jar {
        manifest {
            attributes 'Implementation-Title': 'Gradle Quickstart',
                       'Implementation-Version': version,
                       'Main-Class': 'hello.helloWorld'
        }
    }
    
    0 讨论(0)
  • 2020-11-29 07:37

    I see two problems here, one with sourceSet another with mainClassName.

    1. Either move java source files to src/main/java instead of just src. Or set sourceSet properly by adding the following to build.gradle.

      sourceSets.main.java.srcDirs = ['src']
      
    2. mainClassName should be fully qualified class name, not path.

      mainClassName = "hello.HelloWorld"
      
    0 讨论(0)
提交回复
热议问题