简体   繁体   中英

Running Gradle application with parameters in NetBeans

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?

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.

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:

在此处输入图片说明

Here is what the Project Properties dialog shows:

在此处输入图片说明 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

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

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM