Gradle Groovy project Error: Could not find or load main class

I was able to finally get a Gradle application project working with Eclipse. (For some odd reason, when you use Eclipse to create Gradle project, it creates a library (i.e. non-executable) one, and has no options to choose other project forms.)

I’m able to not only get it working in Eclipse (i.e. no errors when I “Refresh Gradle project”), but am able to gradle clean build it as well.

However, when I try to gradle run it, I face issue:

$ gradle run --args="--jsonConfig cliConfig.json"

> Task :lib:run FAILED
Error: Could not find or load main class com.signaturemd.sposZohoMergeScript.App
Caused by: java.lang.ClassNotFoundException: com.signaturemd.sposZohoMergeScript.App

The build.gradle lives in child lib folder, and is defined to be:

plugins {
    id 'application'
}

application { 
    mainClass = 'com.signaturemd.sposZohoMergeScript.App'
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

// SOURCE: ChatGPT AI
task createJar(type: Jar) {
    manifest {
        attributes 'Main-Class': 'com.signaturemd.sposZohoMergeScript.App'
    }
    baseName = 'SposZohoMergeScript' // deprecated
    from(sourceSets.main.output) {
        include '**/*'
    }
    with jar
}

dependencies {
    // Use JUnit Jupiter for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'

    // This dependency is exported to consumers, that is to say found on their compile classpath.
    // api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:30.1.1-jre'
	
	// https://mvnrepository.com/artifact/org.apache.poi/poi
	implementation 'org.apache.poi:poi:5.2.3'
	// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml
	implementation 'org.apache.poi:poi-ooxml:5.2.3'
	
	// https://mvnrepository.com/artifact/org.apache.groovy/groovy-cli-commons
	implementation 'org.apache.groovy:groovy-cli-commons:4.0.9'
	
	// https://mvnrepository.com/artifact/org.apache.groovy/groovy-json
	implementation 'org.apache.groovy:groovy-json:4.0.9'
	
	
}

tasks.named('test') {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

and my project structure, which I didn’t change from Eclipse Gradle project structure, is like this:

enter image description here

Also, settings.gradle, which lives in project root folder, was not modified in any way from what Eclipse Gradle plugin generated:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * The settings file is used to specify which projects to include in your build.
 *
 * Detailed information about configuring a multi-project build in Gradle can be found
 * in the user manual at https://docs.gradle.org/7.2/userguide/multi_project_builds.html
 */

rootProject.name = 'com.signaturemd.sposZohoMergeScript'
include('lib')

What am I doing wrong in my build.gradle file or my project structure?

You most significant problem is, that you think you can use anything that ChatGPT is producing.
It is a hip toy and that’s all.
Do not use any code it produces, I’ve not yet seen anything helpful it produced.
Just using APIs totally wrong or even using methods that do not exist and never existed.
Besides that its data base is very old in tech terms.

Regarding your not found class, you have your Groovy files in src/main/java instead of src/main/groovy, that is like they would not exist at all, see here: The Groovy Plugin

Besides that, some notes:

  • --args=--jsonConfig --args=cliConfig.json or you will have one argument with value --jsonConfig cliConfig.json, not two arguments.
  • better delete the whole createJar non-sense ChatGPT invented, better instead read the documentation of the application plugin you are using: The Application Plugin
  • don’t make the root project name dotted, what you probably wanted is to set the group to com.signaturemed and the name to sposZohoMergeScript, not that a group makes any difference unless you are writing a library you want to publish

Turns out I need to tell Gradle that I am using Groovy, not Java.

I make the following changes to my build.gradle:

plugins {
    id 'groovy' // added line
    id 'application'
    id 'eclipse' // added line
}

sourceSets.main.java.srcDirs = ['src/main/groovy']

dependencies { 

    // https://mvnrepository.com/artifact/commons-cli/commons-cli
    implementation 'commons-cli:commons-cli:1.5.0' // missing dependency, somehow it was there prior
}

Ah, right, missed that you did not apply it. But you don’t need the eclipse plugin, even if you want to import the project into eclipse and you really shouldn’t configure the Groovy source directory as Java source directory. Especially as the Groovy plugin already supports having Java files in the Groovy source directory and doing joint compilation of your really want to mix’n’match.

ChatGPT answers are also banned from StackOverflow for good reasons: Temporary policy: ChatGPT is banned - Meta Stack Overflow

1 Like