Gradle - 无法找到或加载主类 [英] Gradle - Could not find or load main class
问题描述
我正在尝试使用 Gradle 运行一个非常简单的项目,并在使用 gradlew run 命令
时遇到以下错误:
I'm trying to run a very simple project using Gradle and running into the following error when using the gradlew run command
:
无法找到或加载主类hello.HelloWorld"
这是我的文件结构:
SpringTest
-src
-hello
-HelloWorld.java
-Greeter.java
-build
-libs
-tmp
-gradle
-wrapper
-build.gradle
-gradlew
-gradlew.bat
我排除了 libs 和 tmp 文件夹的内容,因为我认为这不是与此问题相关的信息,但如果需要,我可以将其添加.
I excluded the contents of the libs and tmp folders because I didn't think that would be relevant information for this issue, but I can add it in if need be.
这是我的 build.gradle 文件:
Here is my build.gradle file:
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 属性尝试了各种方法,但似乎没有任何效果.
Any idea on how to fix this issue? I've tried all sorts of things for the mainClassName attribute but nothing seems to work.
推荐答案
我在这里看到两个问题,一个是 sourceSet
另一个是 mainClassName
.
I see two problems here, one with sourceSet
another with mainClassName
.
或者将 java 源文件移动到
src/main/java
而不仅仅是src
.或者通过将以下内容添加到 build.gradle 来正确设置sourceSet
.
Either move java source files to
src/main/java
instead of justsrc
. Or setsourceSet
properly by adding the following to build.gradle.
sourceSets.main.java.srcDirs = ['src']
mainClassName
应该是完全限定的类名,而不是路径.
mainClassName
should be fully qualified class name, not path.
mainClassName = "hello.HelloWorld"
这篇关于Gradle - 无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!