0

I am having difficulties passing JVM arguments in NetBeans to my Gradle project. My previous attempts got no success and maybe someone can help me.

Here is what I have tried so far: I am adding the JVM Argument via right click on project --> Properties --> Build In Tasks --> Run --> Putting the JVM Value in the designated field

-Dtest=mytestvalue

(Unfortunately my reputation is not high enough to add embedded images) When I run the project afterwards via right click and run it display:

Executing: gradle :run
Arguments: [-PcmdLineArgs=, -c, D:\NetBeansProjects\app\settings.gradle]
JVM Arguments: [-Dtest=mytestvalue]

:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:run

10:54:55.899 [main] System.getProperty('test') null

So the arguments are displayed in JVM Arguments: [-Dtest=mytestvalue] but not transferred to the application it seems and System.getProperty('test') results in null. I also tried using custom tasks with the same effect.

If I create a jar file and pass the arguments everything works as expected:

λ java -Dtest=mytestvalue -jar app.jar
System.getProperty('test') mytestvalue

System.getProperty('test') results in mytestvalue.

My current workaround is to set the JVM arguments in the build.gradle file which works fine, but I want to get rid of writing the arguments directly into that file.

I am using Gradle 3.3 and NetBeans 8.2

Michael
  • 393
  • 2
  • 4
  • 20

3 Answers3

1

Thanks to @MarvinFrommhold and his Post https://stackoverflow.com/a/26141841/7220665 I have finally found what I wanted.

I just had to extend the run task with

run {
    systemProperties = System.properties
}

and my arguments are passed through to my application where I can use it.

UPDATE

The approach above works as intended, but if you don´t want to delegate all the properties you can specify the ones you need. For example, you want to set mytestvalue

you pass via NetBeans

-Dtest=mytestvalue

and in build.gradle

run {
    // delegate the property 'mytestvalue' to the jvm 
    systemProperty "mytestvalue", System.getProperty("mytestvalue")

    // confirm that the property has been delegated
    println "mytestvalue: " + systemProperties["mytestvalue"]
}
Community
  • 1
  • 1
Michael
  • 393
  • 2
  • 4
  • 20
  • 1
    Your solution does not work for me. Could not find method run() for arguments [build_28bus5y71kwmo5sgwcrxdkkvy$_run_closure5@37d70d88] on root project – user1278890 Jun 14 '17 at 10:41
  • hm, can you post your build.gradle and describe the steps you made? – Michael Jun 14 '17 at 15:31
0

There is a difference between passing a property to gradle build and passing it to your application. You already know that you can set the property in your build.gradle (it is described in https://docs.gradle.org/current/userguide/application_plugin.html). What you can do is to pass a property to Gradle and inside buildfile look for it and pass it further to your launched application. BTW: when doing this you can pass either system property (-D) or project property (-P) to Gradle as explained in https://docs.gradle.org/current/userguide/build_environment.html#properties

Radim
  • 4,721
  • 1
  • 22
  • 25
-1
 You can right click on the project, and select Properties.

 Click on Run category and insert you configuration in VM Options(not JVM).

VM-argument

-Dtest=testing
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32
  • 4
    This is correct for Maven projects but unfortunately not for Gradle. The dialog looks completely different – Michael Jan 13 '17 at 11:14