diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java index 805499fe59..65c98b8e75 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java @@ -60,10 +60,14 @@ public double get() { try { // only refer to hotspot specific class via reflection to avoid linkage errors - Class.forName("com.sun.management.ThreadMXBean"); - // in reference to JMH's GC profiler (gc.alloc.rate) - registry.add("jvm.gc.alloc", Collections.emptyMap(), - (DoubleSupplier) Class.forName(getClass().getName() + "$HotspotAllocationSupplier").getEnumConstants()[0]); + final Class sunBeanClass = Class.forName("com.sun.management.ThreadMXBean"); + // J9 does contain com.sun.management.ThreadMXBean in classpath + // but the actual MBean it uses (com.ibm.lang.management.internal.ExtendedThreadMXBeanImpl) does not implement it + if (sunBeanClass.isInstance(ManagementFactory.getThreadMXBean())) { + // in reference to JMH's GC profiler (gc.alloc.rate) + registry.add("jvm.gc.alloc", Collections.emptyMap(), + (DoubleSupplier) Class.forName(getClass().getName() + "$HotspotAllocationSupplier").getEnumConstants()[0]); + } } catch (ClassNotFoundException ignore) { } }