WildFly CLI Tech Tip #1: How to read WildFly System Properties

Here is a new section for WildFly CLI gurus. In the first tip we will learn how to gather all System Properties which are available in WildFly using the CLI.

You might be surprised to see that WidFly System Properties are not available through the /system-property path. For example, if you are looking for the “jboss.server.config.dir”, here’s the outcome:

/system-property="jboss.server.config.dir":read-resource
{
    "outcome" => "failed",
    "failure-description" => "WFLYCTL0216: Management resource '[(\"system-property\" => \"jboss.server.config.dir\")]' not found",
    "rolled-back" => true
}

The trick is to use the Platform MBeans which contain the application server’s System Properties as runtime attributes: Here’s how to do it: 

/core-service=platform-mbean/type=runtime:read-attribute(name=system-properties)
{
    "outcome" => "success",
    "result" => {
        "[Standalone]" => "",
        "awt.toolkit" => "sun.awt.X11.XToolkit",
        "file.encoding" => "UTF-8",
        "file.encoding.pkg" => "sun.io",
        "file.separator" => "/",
        "java.awt.graphicsenv" => "sun.awt.X11GraphicsEnvironment",
        "java.awt.headless" => "true",
        "java.awt.printerjob" => "sun.print.PSPrinterJob",
        "java.class.path" => "/home/jboss/jboss-eap-7.2/jboss-modules.jar",
        "java.class.version" => "52.0",
        "java.endorsed.dirs" => "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/endorsed",
        "java.ext.dirs" => "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/ext:/usr/java/packages/lib/ext",
        "java.home" => "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre",
        "java.io.tmpdir" => "/tmp",
        "java.library.path" => "/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib",
        "java.naming.factory.url.pkgs" => "org.jboss.as.naming.interfaces",
        "java.net.preferIPv4Stack" => "true",
        "java.runtime.name" => "OpenJDK Runtime Environment",
        "java.runtime.version" => "1.8.0_191-b13",
        "java.security.auth.login.config" => "jar:file:/home/jboss/jboss-eap-7.2/modules/system/layers/base/org/picketbox/main/picketbox-5.0.3.Final-redhat-00004.jar!/auth.conf",
        "java.specification.name" => "Java Platform API Specification",
        "java.specification.vendor" => "Oracle Corporation",
        "java.specification.version" => "1.8",
        "java.util.logging.manager" => "org.jboss.logmanager.LogManager",
        "java.vendor" => "Oracle Corporation",
        "java.vendor.url" => "http://java.oracle.com/",
        "java.vendor.url.bug" => "http://bugreport.sun.com/bugreport/",
        "java.version" => "1.8.0_191",
        "java.vm.info" => "mixed mode",
        "java.vm.name" => "OpenJDK 64-Bit Server VM",
        "java.vm.specification.name" => "Java Virtual Machine Specification",
        "java.vm.specification.vendor" => "Oracle Corporation",
        "java.vm.specification.version" => "1.8",
        "java.vm.vendor" => "Oracle Corporation",
        "java.vm.version" => "25.191-b13",
        "javax.management.builder.initial" => "org.jboss.as.jmx.PluggableMBeanServerBuilder",
        "jboss.home.dir" => "/home/jboss/jboss-eap-7.2",
        "jboss.host.name" => "fedora",
        "jboss.modules.dir" => "/home/jboss/jboss-eap-7.2/modules",
        "jboss.modules.system.pkgs" => "org.jboss.byteman",
        "jboss.node.name" => "fedora",
        "jboss.qualified.host.name" => "fedora",
        "jboss.server.base.dir" => "/home/jboss/jboss-eap-7.2/standalone",
        "jboss.server.config.dir" => "/home/jboss/jboss-eap-7.2/standalone/configuration",
        "jboss.server.data.dir" => "/home/jboss/jboss-eap-7.2/standalone/data",
        "jboss.server.deploy.dir" => "/home/jboss/jboss-eap-7.2/standalone/data/content",
        "jboss.server.log.dir" => "/home/jboss/jboss-eap-7.2/standalone/log",
        "jboss.server.name" => "fedora",
        "jboss.server.persist.config" => "true",
        "jboss.server.temp.dir" => "/home/jboss/jboss-eap-7.2/standalone/tmp",
        "line.separator" => "
",
        "logging.configuration" => "file:/home/jboss/jboss-eap-7.2/standalone/configuration/logging.properties",
        "module.path" => "/home/jboss/jboss-eap-7.2/modules",
        "org.apache.xml.security.ignoreLineBreaks" => "true",
        "org.jboss.boot.log.file" => "/home/jboss/jboss-eap-7.2/standalone/log/server.log",
        "org.jboss.resolver.warning" => "true",
        "org.jboss.security.context.ThreadLocal" => "true",
        "os.arch" => "amd64",
        "os.name" => "Linux",
        "os.version" => "4.18.16-300.fc29.x86_64",
        "path.separator" => ":",
        "sun.arch.data.model" => "64",
        "sun.boot.class.path" => "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/rt.jar
:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openj
dk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jr
e/lib/jfr.jar:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/classes",
        "sun.boot.library.path" => "/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b13-0.fc29.x86_64/jre/lib/amd64",
        "sun.cpu.endian" => "little",
        "sun.cpu.isalist" => "",
        "sun.desktop" => "gnome",
        "sun.io.unicode.encoding" => "UnicodeLittle",
        "sun.java.command" => "/home/jboss/jboss-eap-7.2/jboss-modules.jar -mp /home/jboss/jboss-eap-7.2/modules org.jboss.as.standalone -Djboss.home.dir=/home/jboss/jbo
ss-eap-7.2 -Djboss.server.base.dir=/home/jboss/jboss-eap-7.2/standalone",
        "sun.java.launcher" => "SUN_STANDARD",
        "sun.jnu.encoding" => "UTF-8",
        "sun.management.compiler" => "HotSpot 64-Bit Tiered Compilers",
        "sun.nio.ch.bugLevel" => "",
        "sun.os.patch.level" => "unknown",
        "user.country" => "US",
        "user.dir" => "/home/jboss/jboss-eap-7.2/bin",
        "user.home" => "/home/francesco",
        "user.language" => "en",
        "user.name" => "francesco",
        "user.timezone" => "Europe/Rome"
    }
}

From there, you can easily gather all the information about the environment where the application server has been started. In my case, for example I’ve used to add users programmatically into a remote Application Server (i.e. into the application-users.properties).

Here is a proof of concept code which does exactly this:

String strServerProperties = "/core-service=platform-mbean/type=runtime:read-attribute(name=system-properties)";
cliCommand = ctx.buildRequest(strServerProperties);
output = executeCommand(ctx, cliCommand);

String configFolder = output.replaceAll("^.*jboss.server.config.dir\" : \"", "").replaceAll("\".*", "");

String usersProperties = configFolder + File.separator + "application-users.properties";
Files.write(Paths.get(usersProperties), "\nadmin=88fj4fgfq48gjdkkgir984d9".getBytes(), StandardOpenOption.APPEND);

Check this tutorial to learn more about using the org.jboss.as.cli.CommandContext API: Discover WildFly – JBoss resources from Java code

Found the article helpful? if so please follow us on Socials