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.
Reasons why Java cannot find the class When you get the message "Could not find or load main class ...", that means that the first step has failed. The java command was not able to find the class. And indeed, the "..." in the message will be the fully qualified class name that java is looking for.
Right Click Spring Boot Project -> Maven -> Update ProjectAnd in the Update Maven Project wizard select the Maven Project that you wanted to update with default update options and click “OK”. After, you do the above step try running your Spring Boot App. Yay! it worked for me.
I see two problems here, one with sourceSet
another with mainClassName
.
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']
mainClassName
should be fully qualified class name, not path.
mainClassName = "hello.HelloWorld"
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' } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With