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.
verify if gradle.properties define right one JAVA_HOVE
org.gradle.java.home=C:\Program Files (x86)\Java\jdk1.8.0_181
or
I see two problems here, one with
sourceSet
another withmainClassName
.Either move java source files to
src/main/java
instead of justsrc
. Or setsourceSet
properly by adding the following to build.gradle.mainClassName
should be fully qualified class name, not path.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 yourHelloWorld.java
class into the package.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)
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.