2

I want to run Gradle application with my own parameter in NetBeans.

Example:

java - jar NameOfJarFile -MyParametere valueOfParameter

And what I must do to run my application with that parameter?

Cœur
  • 37,241
  • 25
  • 195
  • 267
krolik1991
  • 244
  • 1
  • 4
  • 13

4 Answers4

3

Although this is an old thread, it might be interesting to mention that I also had this problem using Netbeans and the Gradle plugin and I found 2 solutions, one of them a 'final' one.

  • Solution 1: not final

    • I added the following line to my build.gradle file:

      cmdLineArgs = '-v -test'

      That seems to do the job.

      When I do a normal Run Main Project or Debug Main Project in NetBeans, all goes well. I get the two parameters as arguments to my main class.

      !!!However!!!: I get an error when I try to run a Clean and Build Main Project. The error is pointing to the cmdLineArgs line I added to my build.gradle file. This is the error message I get:

      A problem occurred evaluating root project 'PublicApiCheckerWithGradle'. Could not set unknown property 'cmdLineArgs' for root project 'PublicApiCheckerWithGradle' of type org.gradle.api.Project.

      I still don't know why this is...

      If someone has a better Gradle understanding than me and knows what causes this strange behaviour, I'm interested to know...

  • Solution 2: final

    • Right-click on the NetBeans project, select Properties and then select Custom Variables in the Categories: section of the dialogue box that opens

    • Add the following line in the Custom Variables: section: cmd-line-args=-v -test

    • Close the dialogue box

      Now I can clean and rebuild, build and debug without any issues. The two arguments are always provided to my main method.

Hopefully it helps someone else too.

Best.

GeertVc
  • 1,914
  • 17
  • 22
1

I realize this is an older thread, but I've just spent most of an afternoon trying to resolve this. Here's what I came up with after pulling parts of various solutions together.

  1. In your build.gradle file, add the following:
tasks.named('run') {
    if ( project.hasProperty("cmdArgs") ) {
        args Eval.me(cmdArgs)
    }
}
  1. Right click on the project and select Properties -> Configurations

  2. Clone the default configuration (I named my new configuration default_cli)

Project Properties - Configurations

  1. Edit your new configuration adding the command line parameters you want to pass to the application as an array called cmdArgs in Properties. In my case, I added cmdArgs=['-c','config.json']

Editing configuration settings

  1. Activate your new configuration and run your application. You should now see the parameters in your main method args array:

Args array values

68wooley
  • 11
  • 1
0

Ninad's answer is not valid, since the question was for a Gradle project. For Gradle projects, the project properties dialog doesn't have those options. Instead it has:

enter image description here

Here is what the Project Properties dialog shows:

enter image description here Unfortunately, this is a question I also have. I have attempted to add a Custom Task that supplies the arguments, but when I do, I get a gradle build error which is attempting to run my first argument as a gradle task. It doesn't seem possible to supply arguments in a NetBeans gradle project.

I tried a workaround by going to the command line and running: "gradle jar" to create a jar. Then I tried the standard method of running a jar: "java -jar jarname.jar arg1 arg2" and I get an error from java that:

no main manifest attribute, in jarName.jar
  • You can use Custom Tasks to pass properties to the build process and modify your build.gradle to pass those properties to your application. See: http://stackoverflow.com/questions/23689054/problems-passing-system-properties-and-parameters-when-running-java-class-via-gr – Paul Jun 05 '15 at 15:55
-10

Right Click your project > properties > select Run in LHS panel > in Arguments textfield - enter parameters with spaces in between.

enter image description here

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
  • 3
    The above is a screenshot of a NetBeans project that does **not** use the Gradle plugin. The OP explicitely addressed a Gradle issue with his NetBeans environment. – GeertVc Aug 15 '18 at 17:30