17

I've installed the Gradle-support plugin in Netbeans and I can build and run the project just fine. When I try to run in debug mode, I get the following output:

Executing: gradle debug

:debug
Cannot execute debug because the property "mainClass" is not defined or empty.

BUILD SUCCESSFUL

Total time: 0.222 secs

I'm using:

 Oracle Java 1.8
 Gradle 1.12
 Netbeans 8.0
 Gradle-Support 1.3.0
 LinuxMint 16

Why can't I run my debugger?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Stephen__T
  • 2,004
  • 3
  • 25
  • 48

4 Answers4

19

Add something like

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.foo.acme.Main'
}

to your build.gradle. It will tell Gradle plugin what class to use when starting your application. Perhaps that should be customizable in the UI but I cannot see it now.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
Radim
  • 4,721
  • 1
  • 22
  • 25
  • This seems to have launched the application but its not linking to native libraries, which causes an exception. Do I need to explicitly set the java.library.path again? – Stephen__T May 02 '14 at 12:16
  • I suppose. Either make sure the working directory is what you expect it to be or set `java.library.path` or possibly `LD_LIBRARY_PATH` env var. I haven't tried it native libraries. – Radim May 02 '14 at 13:11
  • I'll mark this as the answer, I don't have a chance to check the lib path stuff right now but this fixed the original issue. – Stephen__T May 02 '14 at 13:54
  • The runtime arguments are not in the UI as far as I know; this is an open issue on the Netbeans-Gradle plugin. See: [Cannot pass arguments to main method](https://github.com/kelemen/netbeans-gradle-project/issues/194) also there may be other not-yet-IDE type issues, e.g: [Gradle tasks should be started in own process - current directory should be netbeans project dir](https://github.com/kelemen/netbeans-gradle-project/issues/192). – will Jun 21 '15 at 13:32
  • A generic issue with Java applications programming (including the server-side) is that as a developer your Build/Compile environment may require a different Run-time context. So it is not just arguments and working folder; there are libary and ClassPath issues; JNI context, AppServer version and context, etc. It is here I take my hat off to JetBrains they usually seem to get to cover all options. – will Jun 21 '15 at 13:36
11

Another solution to this problem is to create a new debug task. Similar to the gradle run task you can just add the following task to your build.gradle file:

task debug(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Marius W
  • 111
  • 1
  • 3
  • This fixed the problem for me. It is a strange thing. I moved the main() Class sub-project to a different root-project. The `run` command still works. And `debug` _stopped_ working. After defining this `task debug()...` it is clear the problem was the `workingDir` was not being set to the right path. Whereas the `run{ }` task sets the correct `workingDir`. Until now, I thought the debug was a run with a `debug` flag set to `true`. No so apparently. – will May 12 '17 at 13:43
5

In addition to Radim's answer the way to customize the mainClass parameter in the Netbeans UI.

enter image description here

In the properties of a Netbeans gradle project go to "Built-In Tasks/Debug", unclick the "Inherit" and add "-PmainClass=aaa.bbb.ccc" to the arguments.

I guess this should also be done for the run task.

It's the same idea like run/debug single file which already take the selected file as parameter mainClass.

Community
  • 1
  • 1
NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
1

I had a similar problem and was able to fix it setting Options / Miscellaneous / Gradle / Task Execution / Automatic tasks to "NetBeans should not add tasks automatically".

Community
  • 1
  • 1
anothernode
  • 5,100
  • 13
  • 43
  • 62