How do I get the Java classpath?

The RuntimeMXBean.getClassPath() returns the Java class path that is used by the system class loader to search for class files. This method is equivalent to System.getProperty("java.class.path").

Multiple paths in the Java class path are separated by the path separator character of the platform of the Java virtual machine being monitored.

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetClassPath {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        String classPath = bean.getClassPath();
        System.out.println("ClassPath = " + classPath);
    }
}

How do I get system properties information?

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Map;
import java.util.Set;

public class GetSystemProperties {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        // Returns a map of names and values of all system
        // properties. This method calls System.getProperties()
        // to get all system properties. Properties whose
        // name or value is not a String are omitted.
        Map<String, String> systemProperties = bean.getSystemProperties();
        Set<String> keys = systemProperties.keySet();
        for (String key : keys) {
            String value = systemProperties.get(key);
            System.out.printf("Property[%s] = %s%n", key, value);
        }
    }
}

Some properties produced by the code snippet above are:

Property[java.specification.version] = 17
Property[sun.cpu.isalist] = amd64
Property[sun.jnu.encoding] = Cp1252
Property[java.vm.vendor] = Oracle Corporation
Property[sun.arch.data.model] = 64
Property[user.variant] = 
Property[java.vendor.url] = https://java.oracle.com/
...
...
Property[java.vm.info] = mixed mode, sharing.
Property[java.vendor] = Oracle Corporation.
Property[java.vm.version] = 17+35-LTS-2724.
Property[sun.io.unicode.encoding] = UnicodeLittle.
Property[java.class.version] = 61.0.

How do I get the start time of a JVM?

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.Date;

public class GetStartTime {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        // Returns the start time of the Java virtual machine in 
        // milliseconds. This method returns the approximate time 
        // when the Java virtual machine started.
        long startTime = bean.getStartTime();
        Date startDate = new Date(startTime);
        System.out.println("Start Time = " + startTime);
        System.out.println("Start Date = " + startDate);
    }
}

The result of the code snippet above:

Start Time = 1634391703196
Start Date = Sat Oct 16 21:41:43 CST 2021

How do I get the uptime of a JVM?

The following code snippet demonstrates how to get the time for how long has the JVM been running.

package org.kodejava.lang.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetUptime {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        // Returns the uptime of the Java virtual machine in
        // milliseconds.
        long uptime = bean.getUptime();
        System.out.printf("Uptime = %d (ms).", uptime);
    }
}

The output of the code snippet above:

Uptime = 125 (ms).

How do I append and replace content of a StringBuffer?

A StringBuffer is like a String, but can be modified. StringBuffer are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.

As of release JDK 1.5, this class has been supplemented with an equivalent class designed for use by a single thread, the StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all the operations, but it is faster, as it performs no synchronization.

package org.kodejava.lang;

public class StringBufferAppendReplace {
    public static void main(String[] args) {
        StringBuffer quote = new StringBuffer("an apple ");
        char a = 'a';
        String day = " day";
        StringBuffer sentences = new StringBuffer(" keep the doctor away");

        // Append a character into StringBuffer
        quote.append(a);
        System.out.println("quote = " + quote);

        // Append a string into StringBuffer
        quote.append(day);
        System.out.println("quote = " + quote);

        // Append another StringBuffer
        quote.append(sentences);
        System.out.println("quote = " + quote);

        // Replace a sub string from StringBuffer starting
        // from index = 3 to index = 8
        quote.replace(3, 8, "orange");
        System.out.println("quote = " + quote);
    }
}

Here is our program output:

quote = an apple a
quote = an apple a day
quote = an apple a day keep the doctor away
quote = an orange a day keep the doctor away