Main class defined three times?

in this build file:

`apply plugin: 'java’
apply plugin: ‘application’

sourceCompatibility = ‘1.8’
[compileJava, compileTestJava].options.encoding = ‘UTF-8’

// NetBeans will automatically add “run” and “debug” tasks relying on the
// “mainClass” property. You may however define the property prior executing
// tasks by passing a “-PmainClass=<QUALIFIED_CLASS_NAME>” argument.
//
// Note however, that you may define your own “run” and “debug” task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty(‘mainClass’)) {
ext.mainClass = ‘net.bounceme.mordor.Main’
}

mainClassName = ‘net.bounceme.mordor.Main’

repositories {
mavenCentral()
// You may define additional repositories, or even remove “mavenCentral()”.
// Read more about repositories here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
// TODO: Add dependencies here …
// You can read more about how to add dependency here:
// http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
testCompile group: ‘junit’, name: ‘junit’, version: ‘4.10’
}

jar {
manifest {
attributes ‘Main-Class’: ‘net.bounceme.mordor.Main’
}
}

`

the main class is defined three times. Surely, at least one of those is redundant? I want to be able to use, on the CLI, gradle run and gradle build, and then run the JAR, and for Netbeans to be able to run from the IDE.

So far it seems that they’re all required. Surely that’s an error on my part?

see also


git@github.com:THUFIR/hello_client.git

First a little formatting tip: Use triple backticks (```) to insert formatted code. :slight_smile:

That one is unnecessary.

This one can be replaced with

if (!hasProperty('mainClass')) {
    ext.mainClass = mainClassName
}

If you move it down after the mainClassName declaration. I have never used the Netbeans plugin, so I’m not sure whether this line is really needed. You might wanna try removing it and see what happens :slight_smile:

When I comment out the manifest attribute in the jar task:

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ gradle clean build;java -jar build/libs/hello_client.jar
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 1.085 secs

hello world

thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ nano build.gradle 
thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ cat build.gradle 
apply plugin: 'java'
apply plugin: 'application'

sourceCompatibility = '1.8'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

// NetBeans will automatically add "run" and "debug" tasks relying on the
// "mainClass" property. You may however define the property prior executing
// tasks by passing a "-PmainClass=<QUALIFIED_CLASS_NAME>" argument.
//
// Note however, that you may define your own "run" and "debug" task if you
// prefer. In this case NetBeans will not add these tasks but you may rely on
// your own implementation.
if (!hasProperty('mainClass')) {
    ext.mainClass = 'net.bounceme.mordor.Main'
}

mainClassName = 'net.bounceme.mordor.Main'

repositories {
    mavenCentral()
    // You may define additional repositories, or even remove "mavenCentral()".
    // Read more about repositories here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:repositories
}

dependencies {
    // TODO: Add dependencies here ...
    // You can read more about how to add dependency here:
    //   http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
    testCompile group: 'junit', name: 'junit', version: '4.10'
}




jar  {
    manifest {
//        attributes 'Main-Class': 'net.bounceme.mordor.Main'
    }
}
thufir@mordor:~/NetBeansProjects/hello_client$ 
thufir@mordor:~/NetBeansProjects/hello_client$ gradle clean build;java -jar build/libs/hello_client.jar
:clean
:compileJava
:processResources UP-TO-DATE
:classes
:jar
:startScripts
:distTar
:distZip
:assemble
:compileTestJava UP-TO-DATE
:processTestResources UP-TO-DATE
:testClasses UP-TO-DATE
:test UP-TO-DATE
:check UP-TO-DATE
:build

BUILD SUCCESSFUL

Total time: 1.259 secs
no main manifest attribute, in build/libs/hello_client.jar
thufir@mordor:~/NetBeansProjects/hello_client$ 


so, it seems that it’s required, at least to build a runnable JAR. No?

You can just call gradle run to run your app. Gradle will also create a Unix and Windows shells script to start your app.

But if you want an executable jar, then you need the manifest attribute, that’s true. In that case you can replace the duplicated String with the mainClassName property.

1 Like

Greetings,

I had a hit from this just this week. I have eliminated use of “mainClass” in all my Gradle scripts and there was no effect.

Use “mainClassName” everywhere, which is the Gradle Application plug-in value. And add a “main = mainClassName” to your Run and Debug tasks if the IDE isn’t doing that for you. “main” belongs to JavaExec. That’s a 30% improvement :wink:

“mainClass” as far as I know, comes from the Maven model and I’ve found that I don’t need it. One less thing to check.