javasunmxbean

Why sun.management.OperatingSystemImpl is package visible?


This class has really helpful methods. I can call them by reflection. But why I forced to do it?

I would like to cast OperatingSystemMXBean to OperatingSystemImpl and call them normal way.

Thanks in advance.


Solution

  • can you explain why you would like to cast OperatingSystemMXBean to OperatingSystemImpl since OperatingSystemImpl implements OperatingSystemMXBean.

    you can use it

        java.lang.management.OperatingSystemMXBean os = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
        System.out.println(os.getArch());
    

    or

        com.sun.management.OperatingSystemMXBean osBean = ManagementFactory
                .getPlatformMXBean(com.sun.management.OperatingSystemMXBean.class);
        System.out.println(osBean.getProcessCpuLoad() * 100);
        System.out.println(osBean.getSystemCpuLoad() * 100);