Reputation: 382
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?
Upvotes: 0
Views: 1319
Reputation: 42214
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.
Upvotes: 1