We have a section of code that needs to monitor the CPU load. We get the CPU load with the following line of code:
((com.sun.management.OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean()).getSystemCpuLoad();
During Testing, this works fine.
When deployed to a 2012 Windows Server, running as a Service, getSystemCpuLoad always returns -1.
If we stop the service and run it as an application (still on the 2012 Window Server machine), it works again.
I have looked at classpath differences, and environmental variable differences and cannot find any discrepancies.
Does anyone have an idea of why getSystemCpuLoad works when running in an application but not as a service?