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 load main class 'hello.HelloWorld'
Here is my file structure:
SpringTest
-src
-hello
-HelloWorld.java
-Greeter.java
-build
-libs
-tmp
-gradle
-wrapper
-build.gradle
-gradlew
-gradlew.bat
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.
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'
}
Any idea on how to fix this issue? I've tried all sorts of things for the mainClassName attribute but nothing seems to work.
Modify build.gradle to put your main class in the manifest:
In my build.gradle, I resolved this issue by creating a task and then specifying the "mainClassName" as follows:
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
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 findclasses
underbuild
directory.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
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:
Notice that it is part of the package "com.stormtrident".
Gradle build: