WildFly can't see OperatingSystemMXBean?
Asked Answered
D

3

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?

Daddy answered 2/5, 2017 at 11:39 Comment(0)
F
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.

Fraudulent answered 3/5, 2017 at 9:29 Comment(3)
So, WildFly deliberately excluded some classes from JDK? Curious why. JBoss5 and JBoss6 has no problems with those classes.Daddy
@Daddy The com.sun.* packages are private classes written by Sun/Oracle for the Sun JVM and are not part of the Java/JVM spec.Tawny
@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 docs.oracle.com/javase/8/docs/jre/api/management/extension/com/…). In Java 11/12, the package is part of the jdk.management module (again, public API). So, jboss-modules is overly restrictive here.Rochdale
I
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>
Icono answered 6/2, 2018 at 19:15 Comment(1)
This answer solved my queries on windows operating system. Need to verify on AIXWeidar
F
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.

Fraudulent answered 3/5, 2017 at 9:29 Comment(3)
So, WildFly deliberately excluded some classes from JDK? Curious why. JBoss5 and JBoss6 has no problems with those classes.Daddy
@Daddy The com.sun.* packages are private classes written by Sun/Oracle for the Sun JVM and are not part of the Java/JVM spec.Tawny
@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 docs.oracle.com/javase/8/docs/jre/api/management/extension/com/…). In Java 11/12, the package is part of the jdk.management module (again, public API). So, jboss-modules is overly restrictive here.Rochdale
D
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);
Daddy answered 4/5, 2017 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.