Setting system properties in Groovy

smeeb picture smeeb · Mar 16, 2016 · Viewed 12.5k times · Source

Please note: Although I mention Swing and MacOS here, this question has nothing to do with either of them: I'm just providing them as a concrete example of what I'm trying to do.


I'm trying to set a system property the groovy way. If you are developing a Swing app on a Mac, it is common practice to set the following system property so that your Swing app's menu looks the same as typical Mac apps:

System.setProperty("apple.laf.useScreenMenuBar", "true")

When I call that inside my main method, it has the desired effect (the menu bar gets pulled off of the JFrame and is pinned to the top of the screen).

But when I go to try and make that call groovier:

System.properties['apple.laf.useScreenMenuBar', 'true']

it doesn't work. No exceptions, it just stops working and doesn't have the desired effect in the UI. Why, and what can I do to fix it?

Answer

Opal picture Opal · Mar 16, 2016

Should be:

System.properties['apple.laf.useScreenMenuBar'] = true

or

System.properties.'apple.laf.useScreenMenuBar' = true 

In this piece of code:

System.properties['apple.laf.useScreenMenuBar', 'true']

['apple.laf.useScreenMenuBar', 'true'] is taken as the key. See below:

def m = [ [1, 2,]:3, 2:4 ]
assert m[1, 2] == 3

The following piece of code returns correct results:

System.properties['lol'] = 2

assert 2 == System.properties['lol']