Skip to content

Instantly share code, notes, and snippets.

@rmorenobello
Last active November 2, 2020 04:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmorenobello/4bfaad1df7fcdc93f60d to your computer and use it in GitHub Desktop.
Save rmorenobello/4bfaad1df7fcdc93f60d to your computer and use it in GitHub Desktop.
How to set and retrieve JVM system variables when running java/groovy from CLI
// http://stackoverflow.com/questions/8098564/is-it-possible-to-specify-java-system-properties-when-running-groovy-from-comman
/*
System properties are set on the java command line using the
java -Dpropertyname=value
syntax.
They can also be added at runtime using
System.setProperty(name, value)
or via the various
System.getProperties().load()
methods.
System.getProperties()
lists all java system properties, and those set from command line will be there,
but there's no way to distinguish those from the other properties added by the system.
Note that you can also set system properties with the environment variable JAVA_TOOL_OPTIONS
Environment variables are set in the OS, eg in linux
export HOME=/Users/myusername
or on windows
SET WINDIR=C:\Windows
etc., and, unlike system properties, may not be set at runtime.
Java chose to expose OS environment variables as Properties with
System.getEnv() or System.getEnv(variable)
(just as Java exposes current directory and "other stuff" as Properties).
*/
/* SET A JAVA/GROOVY SYSTEM VARIABLE FROM CLI
* Exactly as in java:
* java -Dproperty_name="property_value appName.java
*/
groovy -Dproperty_name="property_value" myGroovy.groovy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment