0

I've been attempting to run a java main from Gradle however Gradle keeps giving me the error

Execution failed for task ':myTask'

No main class specified

I've attached the code below

sourceSets {
    main {
        java {
            srcDirs 'src/myPackage/downloadupdater'
            srcDirs 'src/myPackage/downloadupdater/util' 
            srcDirs 'src/myPackage/downloadupdater/dao'
        }
        // output.classesDir = "/bin"
    }
}

task myTask(dependsOn : compileJava, type : JavaExec){
    group = "Custom"
    description = "Acquires the weekly stats"

    doLast{
        classpath = sourceSets.main.output.classesDir
        main = "myPackage.SomeClass"
        args "-w"
    }
}

The java class I'm calling is:

package myPackage
public class SomeClass{

    private static DownloadUpdater updater;

    public static void main (String [] args) {

        updater = new DownloadUpdater();
    
        updater.whichStatistics(args[0]);
    
        updater.setCalendar();
    
        updater.secureWorkbook();
    
        updater.getStatistics();

    }
}

After building, my directories come up with a build-classes-java-main-myPackage but gradle still can't find the main class.

Community
  • 1
  • 1
Cheabs
  • 37
  • 6

1 Answers1

1

Either move java source files to src/main/java instead of just src. Or set

sourceSet properly
sourceSets.main.java.srcDirs = ['src'] 

and use

task execute(type:JavaExec) {
main = "myPackage.SomeClass"
classpath = sourceSets.main.runtimeClasspath
}

for more refer : Gradle to execute Java class (without modifying build.gradle)

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17
  • Realized my mistake, I was setting the main and classpath inside a doLast block which was throwing an error when trying to configure the task as a Java Executable. Once moved out of the doLast block and allowed to run during configuration phase, everything worked out. Thanks! – Cheabs Jul 06 '17 at 17:29
  • Error:(38, 1) A problem occurred evaluating root project 'MyProject'. > Could not get unknown property 'sourceSets' for task ':execute' of type org.gradle.api.tasks.JavaExec. – Zon Jan 19 '18 at 18:04