How to use getSystemCpuLoad () in JMX

Hi, I cannot use getProcessCpuTime () or getProcessCpuLoad () or getSystemCpuLoad () in my java program. I used as shown below.

ManagementFactory. getOperatingSystemMXBean().getProcessCpuTime(); 

also like

 ( (OperatingSystemMXBean) getOperatingSystemMXBean() ).getProcessCpuTime() 

But its error display, like the getProcessCpuTime () method, was not found. I have included the following header files. Is this enough or do I need to use more.?

 import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; 

Can anyone suggest me how to use these methods. I am using jdk1.6. And my code is below

 import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class printUsage { public static void main(String[] args) { OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean(); System.out.println("getCpuProcessTime()" + " = " + operatingSystemMXBean.getProcessCpuTime()); System.out.println("getCpuProcessTime()" + " = " + operatingSystemMXBean.getSystemCpuLoad()); System.out.println("getCpuProcessTime()" + " = " + operatingSystemMXBean.getProcessCpuLoad()); } } 
+6
source share
2 answers

import next

  import com.sun.management.OperatingSystemMXBean; 

Not

 import java.lang.management.OperatingSystemMXBean; 

Use the following code.

 OperatingSystemMXBean operatingSystemMXBean = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean(); 

I get reliable results with this code.

+11
source

Take a look at the following javadoc: http://docs.oracle.com/javase/6/docs/api/

See which methods are defined for OperatingSystemMXBean. Your problem is that you are looking for methods defined in com.sun.management.OperatingSystemMXBean , in java.lang.management.OperatingSystemMXBean .

The com.sun package com.sun intended primarily for internal use by java creators, and not for application developers. You should use the classes from the java.lang.management and refer to the corresponding API document.

+3
source

Source: https://habr.com/ru/post/908058/


All Articles