elastic / elastic/apm-agent-java

UnsupportedOperationException at startup when using JDK 26+

Open
#4,519 1 comment 0 reactions 0 assignees View on GitHub
agent-java bug
Dominant language
Java
Stars
594
Forks
338
Avg merge
1d 13h
Merged PRs (30d)
25

Description

Initially reported [in our forum](https://discuss.elastic.co/t/instrumentation-failure-with-java-agent-1-56/387319).

```
2026-06-25 17:22:20,878 [main] ERROR co.elastic.apm.agent.bci.ElasticApmAgent - Exception occurred while applying instrumentation co.elastic.apm.agent.java_ldap.LdapClientInstrumentation
java.lang.RuntimeException: java.lang.UnsupportedOperationException: Could not access Unsafe class: As of Java 26, using Unsafe is disabled by default, set net.bytebuddy.safe to true if you want to use the JVM's internal unsafe API even though it will become unsupported in the future and should be replaced by injection using method handles: net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup
at co.elastic.apm.agent.bci.IndyBootstrap.getIndyBootstrapMethod(IndyBootstrap.java:240) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.ElasticApmAgent.getTransformer(ElasticApmAgent.java:486) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.ElasticApmAgent.applyAdvice(ElasticApmAgent.java:444) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.ElasticApmAgent.initAgentBuilder(ElasticApmAgent.java:365) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.ElasticApmAgent.initInstrumentation(ElasticApmAgent.java:290) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.ElasticApmAgent.initInstrumentation(ElasticApmAgent.java:179) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.ElasticApmAgent.initialize(ElasticApmAgent.java:165) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:565) ~[?:?]
at co.elastic.apm.agent.premain.AgentMain.loadAndInitializeAgent(AgentMain.java:157) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.premain.AgentMain.init(AgentMain.java:98) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.premain.AgentMain.premain(AgentMain.java:47) [elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:565) ~[?:?]
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:544) [?:?]
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:556) [?:?]
Caused by: java.lang.UnsupportedOperationException: Could not access Unsafe class: As of Java 26, using Unsafe is disabled by default, set net.bytebuddy.safe to true if you want to use the JVM's internal unsafe API even though it will become unsupported in the future and should be replaced by injection using method handles: net.bytebuddy.dynamic.loading.ClassInjector$UsingLookup
at net.bytebuddy.dynamic.loading.ClassInjector$UsingUnsafe$Dispatcher$Unavailable.initialize(ClassInjector.java:2206) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at net.bytebuddy.dynamic.loading.ClassInjector$UsingUnsafe.injectRaw(ClassInjector.java:1907) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at net.bytebuddy.dynamic.loading.ClassInjector$AbstractBase.injectRaw(ClassInjector.java:202) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.IndyBootstrap.loadClassInBootstrap(IndyBootstrap.java:310) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.IndyBootstrap.initIndyBootstrap(IndyBootstrap.java:268) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
at co.elastic.apm.agent.bci.IndyBootstrap.getIndyBootstrapMethod(IndyBootstrap.java:234) ~[elastic-apm-agent-1.56.1-SNAPSHOT.jar:1.56.1-SNAPSHOT]
... 15 more
```

I think we need to modify the call to `ClassInjector` in`co.elastic.apm.agent.bci.IndyBootstrap#loadClassInBootstrap` method to implement something close to what is being done in upstream otel in https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/31188f0f8190356f8b943c81a0c8c51e9a45e6c4/muzzle/src/main/java/io/opentelemetry/javaagent/tooling/HelperInjector.java#L391

As reference, here is the PR that mainly contributed to the current version (where Unsafe usages were being removed) : https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/14855

The temporary workaround is to set `net.bytebuddy.safe` = `false` in Java system properties by adding the `-Dnet.bytebuddy.safe=false` JVM command line argument.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.