Retrieving a few of system variables is inevitable in developing computer applications within any environment. In this post, we would show you system variables existing once the application is loaded and how to retrieve a specific variable to pass it to another routine.
To do so, the following statements will print out all the existing values of System variables.
System.properties.each { k, v -> println "$k = $v" }
The result often looks like
java.vendor = Oracle Corporation sun.java.launcher = SUN_STANDARD catalina.base = C:\Users\tnguyen\EBI\Projects\jummp-biomodels\target\work\tomcat sun.nio.ch.bugLevel = sun.management.compiler = HotSpot 64-Bit Tiered Compilers catalina.useNaming = true os.name = Windows 10 sun.boot.class.path = C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\classes org.mortbay.xml.XmlParser.NotValidating = true sun.desktop = windows grails.env.default = true java.vm.specification.vendor = Oracle Corporation java.runtime.version = 1.8.0_162-b12 grails.env.initializing = false grails.env = development user.name = tnguyen java.naming.factory.initial = org.apache.naming.java.javaURLContextFactory user.language = en jdk.reflect.allowGetCallerClass = true sun.boot.library.path = C:\Program Files\Java\jdk1.8.0_162\jre\bin javax.sql.DataSource.Factory = org.apache.commons.dbcp.BasicDataSourceFactory java.version = 1.8.0_162 user.timezone = Europe/London sun.arch.data.model = 64 jline.shutdownhook = false java.endorsed.dirs = C:\Program Files\Java\jdk1.8.0_162\jre\lib\endorsed sun.cpu.isalist = amd64 sun.jnu.encoding = Cp1252 file.encoding.pkg = sun.io file.separator = \ java.specification.name = Java Platform API Specification java.class.version = 52.0 grails.server.factory = org.grails.plugins.tomcat.TomcatServerFactory user.country = GB java.home = C:\Program Files\Java\jdk1.8.0_162\jre grails.home = C:\Users\tnguyen\devtools\grails-2.5.5 java.vm.info = mixed mode os.version = 10.0 grails.build.execution.context = C:\Users\tnguyen\AppData\Local\Temp\jummp-biomodels3048486709134205985grails-execution-context grails.version = 2.5.5 path.separator = ; java.vm.version = 25.162-b12 grails.disable.exit = true user.variant = grails.env.set = true grails.fork.active = true java.awt.printerjob = sun.awt.windows.WPrinterJob grails.shutdown.hook.installed = true sun.io.unicode.encoding = UnicodeLittle awt.toolkit = sun.awt.windows.WToolkit jummp.basePath = C:\Users\tnguyen\EBI\Projects\jummp-biomodels\. user.script = java.naming.factory.url.pkgs = org.apache.naming user.home = C:\Users\tnguyen java.specification.vendor = Oracle Corporation springloaded = profile=grails;cacheDir=C:\Users\tnguyen/.grails/2.5.5/ java.library.path =...
Let’s give an example.
We often want to get the directory of the currently logged-in user to make other directories to store exported files or backup data, for instance. To do so, the snippet below may help.
String tmpDirPath = System.getProperty("java.io.tmpdir") String userHomePath = System.getProperty("user.home") // We also do like the following def fileSeparator = System.properties.'file.separator' def log4jFileName = System.properties.'catalina.base' + "${fileSeparator}logs${fileSeparator}${appName}.log"
Setting System property from the command line in grails
In one of my recent grails project, I have needed to set System property from the command line while running the grails application. Presumably, we want to set any property, say app.property=”propertyValue”, then the command to set the property in grails application would be:
grails -Dapp.property="propertyValue" run-app
Similarly to retrieve the value of System property, we use
String myPropertyValue= System.getProperty("app.property")
Another example could be liked this
log4jFileName = (System.properties.'catalina.base') + "/logs/${appName}.log" file: "${config.log4jFileName}"
I found the post on the blog of the developer of Grails core’s ones which is discussing quite a lot of useful tips and tricks for retrieving the application’s properties. The copyright of the featured image comes from that blog.
Hope it helps you to solve your problem.