Search code examples
javawildflywildfly-10

WildFly can't see OperatingSystemMXBean?


I have this code to read physical memory:

com.sun.management.OperatingSystemMXBean os = (com.sun.management.OperatingSystemMXBean)ManagementFactory.getOperatingSystemMXBean();
long physicalMemorySize =os.getTotalPhysicalMemorySize();
System.out.println("physicalMemorySize="+physicalMemorySize);

I have JDK 1.8.0_121 (64bit, on Windows)
This code is compiled without problem, and I can run it in console application, it runs OK.
But when I put this code to some Bean or JSP page on WildFly 10 server, it shows error:

Caused by: java.lang.ClassNotFoundException: com.sun.management.OperatingSystemMXBean

WildFly uses exactly the same JDK, so it should see this class like console application sees it.
That class is in jdk1.8.0_121\jre\lib\rt.jar so I do not understand why WildFly complains about that ClassNotFoundException.

What's the problem? How to make WildFly run that code?


Solution

  • That's because those packages are filtered out by jboss-modules if you look into "modules/system/layers/base/sun/jdk/main/module.xml" you can see that com.sun.management is not there. You need to create a module to get those classes or edit this module.