Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not set unknown property 'mainClass' for extension 'application

I'm using Gradle version 6.1 in my project.

The build.gradle for the project is given below :

plugins {
    id 'application'
}

application {
    mainClass = 'com.mytestproject.Main'
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

group 'com.mytestproject'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

But when I run this command ./gradlew assemble, it is giving me error given below :

./gradlew assemble

FAILURE: Build failed with an exception.

* Where:
Build file '/com/myproject/build.gradle' line: 6

* What went wrong:
A problem occurred evaluating root project 'JavaTestProject'.
> Could not set unknown property 'mainClass' for extension 'application' of type org.gradle.api.plugins.internal.DefaultJavaApplication.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 484ms
like image 671
Alexei Avatar asked May 23 '20 18:05

Alexei


3 Answers

You could try this (based off the source code):

application {
    setMainClassName("some.package.Main")
}
like image 97
xjcl Avatar answered Nov 15 '22 12:11

xjcl


Modify build.gradle to this :

apply plugin: "application"

mainClassName = "com.mytestproject.Main"

sourceCompatibility = 1.8
targetCompatibility = 1.8

group 'com.mytestproject'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

Run this commmand : ./gradlew build

like image 10
Anish B. Avatar answered Nov 15 '22 13:11

Anish B.


Actually just modifying the application block to

application {
    mainClassName = 'com.mytestproject.Main'
}

does the same thing.

The problem seems to be that there used to be a mainClass attribute on the org.gradle.api.plugins.internal.DefaultJavaApplication class, but it later got renamed to mainClassName, rendering previous tutorials invalid.

like image 1
darklight Avatar answered Nov 15 '22 12:11

darklight