Reputation: 72636
I've written an application in java and I want to add a feature to report the uptime of the application. Is there a Class/method in the JVM that can do this?
Should I save the timestamp when application starts, then calculate the difference with the current uptime in the moment of the request?
What's a better way to retrieve application uptime in Java?
Upvotes: 23
Views: 23180
Reputation: 3473
You can use RuntimeMXBean.getUptime()
RuntimeMXBean rb = ManagementFactory.getRuntimeMXBean();
long uptime = rb.getUptime();
Upvotes: 48
Reputation: 340733
The result is in milliseconds:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getUptime());
See: ManagementFactory.getRuntimeMXBean() javadoc.
Upvotes: 1
Reputation: 843
The solution I know is to use System.currentTimeMillis()
as it's described here and here. There also was similar question
Upvotes: -6
Reputation: 2209
It is just like Heartbeat application. IN which You have write small application which communicate to your java application. Whenever You application start you have to store the time with date in file/database. Monitor application will check the application running or not. The monitor application also take difference of end time and start time sand display the application time.
Upvotes: -1