Java: How to Get the Start Time of a JVM ?

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » Java: How to Get the Start Time of a JVM ?

JVM_on_top_of_OS - Crunchify Tips

The ManagementFactory class is a factory class for getting managed beans for the Java platform. This class consists of static methods each of which returns one or more platform MXBean(s) representing the management interface of a component of the Java virtual machine.

An application can access a platform MXBean in the following ways:

  • Direct access to an MXBean interface
    1. Get the MXBean instance through the static factory method and access the MXBean locally of the running virtual machine.
    2. Construct an MXBean proxy instance that forwards the method calls to a given MBeanServer by calling newPlatfromMXBeanProxy. A proxy is typically constructed to remotely access an MXBean of another running virtual machine.
  • Indirect access to an MXBean interface via MBeanServer
    1. Go through the platform MBeanServer to access MXBeans locally or a specific MBeanServerConnection to access MXBeans remotely. The attributes and operations of an MXBean use only JMX open types which include basic data typesCompositeData, and TabularData defined in OpenType. The mapping is specified below.

Below is a simple Java Program which returns Start Time and Date of a JVM.

package com.crunchify.tutorials;

/**
 * @author crunchify.com
 */

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

public class CrunchifyGetJVMTime {
    public static void main(String[] args) {
        RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
        long startTime = runtimeBean.getStartTime();
        Date startDate = new Date(startTime);

        System.out.println("\nStart Time in millisecond = " + startTime);
        System.out.println("Start Date = " + startDate);
    }
}

Output:

Start Time = 1367269277031
Start Date = Mon Apr 29 14:01:17 PDT 2013

Leave a Comment