6

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?

Chupacabras
  • 392
  • 1
  • 5
  • 20

3 Answers3

8

This can be addressed by explicitly including com.sun.management classes using something like this in your jboss-deployment-structure.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
    <deployment>
        <dependencies>
            <system>
                <paths>
                    <path name="com/sun/management"/>
                </paths>
            </system>
        </dependencies>
    </deployment>
</jboss-deployment-structure>
Vedran Pavic
  • 2,339
  • 2
  • 27
  • 31
4

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.

ehsavoie
  • 3,126
  • 1
  • 16
  • 14
  • So, WildFly deliberately excluded some classes from JDK? Curious why. JBoss5 and JBoss6 has no problems with those classes. – Chupacabras May 03 '17 at 10:21
  • @Chupacabras The `com.sun.*` packages are private classes written by Sun/Oracle for the Sun JVM and are not part of the Java/JVM spec. – Rüdiger Herrmann Nov 01 '17 at 14:34
  • 1
    @RüdigerHerrmann, while you should be cautious with `com.sun.*` packages in general, `com.sun.management` is marked as `@Exported` package (in Java 8), meaning that the _"package is an exported part of the JDK suitable for use outside of the JDK implementation itself"_ (see https://docs.oracle.com/javase/8/docs/jre/api/management/extension/com/sun/management/package-summary.html). In Java 11/12, the package is part of the `jdk.management` module (again, public API). So, jboss-modules is overly restrictive here. – MyKey_ Aug 26 '19 at 18:33
0

I had to replace my original code, this is the new one:

MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Object attribute =mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"), "TotalPhysicalMemorySize");
long physicalMemorySize = Long.parseLong(attribute.toString());
System.out.println("physicalMemorySize="+physicalMemorySize);
Chupacabras
  • 392
  • 1
  • 5
  • 20