Programmatic API setup to self-attachedit

This installation method is in beta.

Supported environmentsedit

The attachment is supported on Windows, Unix and Solaris operating systems on HotSpot-based JVMs (like OpenJDK and Oracle JDK) and OpenJ9.


The approach to mitigate NoClassDefFoundError s when used in OSGi containers (which includes most application servers) is experimental. Versions prior to 1.18.0 don’t support OSGi containers.

There can only be one agent instance with one configuration per JVM. So if you deploy multiple web applications to the same application server and call ElasticApmAttacher.attach() in each application, the first attach() wins and the second one will be ignored. That also means that if you are configuring the agent with, the application which attaches first gets to decide the configuration. See the default value description of the service_name configuration option for ways to have different ``s for each deployment.

The apm-agent-attach artifact has a transitive dependency on JNA which can be excluded most of the time when running the application with a JDK. However, it will be required in the following cases:

  • When attaching to a JRE and no other copy of JNA is present on the application classpath
  • When attaching to a JDK fails and using JRE attach strategy as fallback.


Declare a dependency to the apm-agent-attach artifact.




compile "co.elastic.apm:apm-agent-attach:$elasticApmVersion"

Call ElasticApmAttacher.attach() in the first line of your public static void main(String[] args) method.

This example demonstrates the the usage of the attach API with a simple Spring Boot application:

import co.elastic.apm.attach.ElasticApmAttacher;
import org.springframework.boot.SpringApplication;

public class MyApplication {
    public static void main(String[] args) {
        ElasticApmAttacher.attach();, args);

The API is not limited to Spring Boot and does not require Spring Boot, it is just used for demonstration purposes.


The recommended way of configuring the agent when using the attach API is to add the configuration to src/main/resources/