0

Here we have the following issue while setting a java-system properties.

Env: Groovy Version: 2.4.12 JVM: 1.8.0_141 Vendor: Oracle Corporation OS: Windows 7

a) Setting and printing a system property in one Groovy script, for e.g. setprop.groovy works:

System.properties.'abc' = '123'
assert '123' == System.properties['abc']
println System.properties["abc"]

Result: 123

b) Trying read previously set property from another JVM-Spawn, for e.g. getprop.groovy doesn't work:

println System.properties["abc"]

Result: null

It seems like setting the property is not really persistent. What shall I have to do, to save java environment variable really persistent, within groovy?

CodeFreezr
  • 362
  • 2
  • 18
  • How are you running your scripts? This question is lacking context and a simple example showing your problem – tim_yates Feb 13 '18 at 18:37
  • I have created two groovy files and run them from shell. I'll tried it inside git.bash, cmd and powershell. – CodeFreezr Feb 13 '18 at 22:08
  • Then the answer below is correct – tim_yates Feb 13 '18 at 22:12
  • My Problem is not solved with the answer by Szymon, even his writing are very inspiring. I'm looking for a solution, from a groovy script, which makes an environment property persistent. The second groovy is just a test to read a variable not inside the same JVM-Spawn. It could read by maven, gradle, ant or what ever outside the JVM within I wanna set the Environment-Property. – CodeFreezr Feb 13 '18 at 22:20
  • It sounds like you haven't defined your question precisely. Below you can find detailed answer why your expectation about setting system properties is wrong. You can check this question on how to try to export env variable from Java program (it's a hack, not a solution) - https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java Alternatively you can share data between programs using files, database, queues etc. If you're looking for something quick and simple - write your data to a file and read it from other program. And be aware of possible race conditions. – Szymon Stepniak Feb 13 '18 at 23:03
  • I tried to explain my question as simple and clear as possible. Sorry If I did not hit it perfectly. I thought the headline "Groovylang: setting system properties are not persistent?" makes clear that "system property" is the main fokus. Thanks for the link to the java solutions with all that boilerplate. I hope someone has an Idea for an more groovy'sh style of solution. – CodeFreezr Feb 13 '18 at 23:30
  • I updated my question a bit to sharpen the focus better. – CodeFreezr Feb 13 '18 at 23:33

1 Answers1

1

System.properties refers to properties available for the JVM process that runs your script. When you run a Groovy script it spawns a VM and runs the script inside this VM, e.g.

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.fc26.x86_64/bin/java -classpath /home/wololock/.sdkman/candidates/groovy/current/lib/groovy-2.4.12.jar -Dscript.name=/home/wololock/.sdkman/candidates/groovy/current/bin/groovy -Dprogram.name=groovy -Dgroovy.starter.conf=/home/wololock/.sdkman/candidates/groovy/current/conf/groovy-starter.conf -Dgroovy.home=/home/wololock/.sdkman/candidates/groovy/current -Dtools.jar=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-0.b14.fc26.x86_64/lib/tools.jar org.codehaus.groovy.tools.GroovyStarter --main groovy.ui.GroovyMain --conf /home/wololock/.sdkman/candidates/groovy/current/conf/groovy-starter.conf --classpath . test1.groovy

This is what process of running groovy test1.groovy script looks like in Linux. In your case test2.groovy would be able to access System.properties['abc'] if you run test2.groovy script inside VM spawned by test1.groovy script, e.g.

System.properties.'abc' = '123'
assert '123' == System.properties['abc']
println System.properties["abc"]

GroovyShell shell = new GroovyShell()
shell.parse(new File('test2.groovy')).run()

In this example I have run test2.groovy using GroovyShell and what I got in the console is:

123
123

The first 123 gets printed by test1.groovy script, second one gets printed by test2.groovy script.

You could even try adding Thread.sleep(10000) (sleeps for 10 seconds) and run both scripts in parallel and list processes that run groovy - you will see two VM spawned that don't share properties between each other.

If you want to get value from one script to another I would suggest returning this value from the first script and passing it as a parameter to the second script.

Szymon Stepniak
  • 40,216
  • 10
  • 104
  • 131
  • Thanks a lot for your effort. It's not about sharing information between groovy-scripts. It's about to store a java environment property to have them also for other java application, even after a restart of a system. – CodeFreezr Feb 13 '18 at 22:14
  • I accept this answer, because it stated implicit that environment property in groovy are simply **not** persistent. For further details I will raise another Question. Thank you @SzymonStepniak for your patient. This was really my 2nd Question here on StackOverflow. – CodeFreezr Feb 14 '18 at 17:23