2

I have configured my Eclipse to use JRE 8. I confirmed it in buildPath.

The compiler is specified as 1.8.

I am trying to get the SystemCpuLoad. Here is what I have.

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

    OperatingSystemMXBean operatingSystemBean = ManagementFactory.getOperatingSystemMXBean();

    operatingSystemBean.getSystemCpuLoad();

Eclipse complains saying "no getSystemCpuLoad()" method. I thought it could be an eclipse issue and try using maven. still build fails with the following error.

java:[221,60] cannot find symbol
[ERROR] symbol:   method getSystemCpuLoad()
[ERROR] location: variable operatingSystemBean of type java.lang.management.OperatingSystemMXBean

I am unable to figure what is going wrong?

EDIT:

In the documentation it says Platform-specific management interface for the operating system on which the Java virtual machine is running.

which means some methods are available in one OS than others. but how can I handle it during compile time to run in linux or mac?

I am currently using mac.

brain storm
  • 30,124
  • 69
  • 225
  • 393

1 Answers1

3

The call to

ManagementFactory.getOperatingSystemMXBean();

returns an instance of java.lang.management.OperatingSystemMXBean

To access the com.sun.management.OperatingSystemMXBean functionality you will need to cast to com.sun.management.OperatingSystemMXBean.

Stephen Souness
  • 272
  • 1
  • 3
  • yes, I figured that out. but for some reasons, I unable to import sun package although it is in JRE library. please see this post. http://stackoverflow.com/questions/32619229/unable-to-import-org-sun-management-operatingsystemmxbean-in-java-class – brain storm Sep 16 '15 at 21:41