Could not set unknown property 'mainClass' for extension 'application
Asked Answered
P

4

10

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
Pummel answered 23/5, 2020 at 18:29 Comment(0)
D
10

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

Domiciliary answered 23/5, 2020 at 18:50 Comment(4)
But why is that? I was following docs and got that error: docs.gradle.org/current/userguide/application_plugin.htmlObtrude
@Daniel Cheung; the documentation you link to is for the current Gradle version, which on this day is 6.4.1. You may be using an older version of Gradle, e.g. in version 6.1 the property name is mainClassname (docs.gradle.org/6.1/userguide/application_plugin.html)Checkrow
@Checkrow Thanks, I didn't realize this was changed across versions.Obtrude
@C0deAttack: Just in case anyone stumbles over this as I did In 6.1. the correct property name is mainClassName not mainClassname ...Marcasite
F
2

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

application {
    setMainClassName("some.package.Main")
}
Ferrara answered 17/12, 2020 at 2:53 Comment(0)
R
2

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.

Rustin answered 11/2, 2022 at 10:57 Comment(0)
I
0

There was a deprecation introduced in 6.4. It went from:

 application {
   mainClassName = "com.foo.bar.FooBar"
 }

To:

 application {
   mainClass.set("com.foo.bar.FooBar")
 }

The Docs:

https://docs.gradle.org/current/javadoc/org/gradle/api/plugins/JavaApplication.html#getMainClassName--

Inrush answered 24/4, 2021 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.