Michael
Michael

Reputation: 483

JVM Arguments in NetBeans with gradle

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

Upvotes: 0

Views: 2080

Answers (3)

Michael
Michael

Reputation: 483

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"]
}

Upvotes: 1

Radim
Radim

Reputation: 4808

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

Upvotes: 0

Jekin Kalariya
Jekin Kalariya

Reputation: 3507

 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

Upvotes: -1

Related Questions