Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle - Could not find or load main class

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.

like image 534
kibowki Avatar asked Jul 24 '14 04:07

kibowki


People also ask

Could not find or load main class main Java?

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.

How Do You Solve could not find or load main class in spring boot?

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.


2 Answers

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" 
like image 167
kdabir Avatar answered Sep 18 '22 21:09

kdabir


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'     } } 
like image 25
Noumenon Avatar answered Sep 20 '22 21:09

Noumenon