# Apm java agent could not find or load main class

**URL:** <https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484>\
**Category:** APM\
**Tags:** docker, java\
**Created:** [January 19, 2023, 7:56am UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484 "2023-01-19T07:56:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![irivas95](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@irivas95](https://discuss.elastic.co/u/irivas95)\
**Post date:** [January 19, 2023, 7:56am UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/1 "2023-01-19T07:56:18Z")

</div>

hi,  
i am trying to mount an application in gke with java apm-agent, with the following dockerfile:

```auto
FROM adoptopenjdk:11-jre-hotspot
RUN export
RUN apt-get -qq update \
   && apt-get install --no-install-recommends -y -qq curl \
   && rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true
WORKDIR /app
COPY . ./
ENV JAVA_OPTS="-Xmx512m"
LABEL \
      org.label-schema.schema-version="1.0" \
      org.label-schema.vendor="Elastic" \
      org.label-schema.name="opbeans-java" \
      org.label-schema.version="1.18.1" \
      org.label-schema.url="https://hub.docker.com/r/opbeans/opbeans-java" \
      org.label-schema.vcs-url="https://github.com/elastic/opbeans-java" \
      org.label-schema.license="MIT"

CMD java $JAVA_OPTS –javaagent:elastic-apm-agent-1.19.0.jar -Dspring.profiles.active=${OPBEANS_JAVA_PROFILE:-}\
                 -Dserver.port=${OPBEANS_SERVER_PORT:-}\
                 -Dserver.address=${OPBEANS_SERVER_ADDRESS:-0.0.0.0}\
                 -Dspring.datasource.url=${DATABASE_URL:-}\
                 -Dspring.datasource.driverClassName=${DATABASE_DRIVER:-}\
                 -Dspring.jpa.database=${DATABASE_DIALECT:-}\
                 -jar /app/opbeans-0.0.1-SNAPSHOT.jar

```

when i try to deploy it i get the following error:

"Error: Could not find or load main class -javaagent:.app.elastic-apm-agent-1.19.0.jar Caused by: java.lang.ClassNotFoundException: -javaagent:.app.elastic-apm-agent-1.19.0.jar"

I am sure I have the elastic-apm-agent-1.19.0.jar file in the same path where I have the Dockerfile when I create the image. I am also sure that the file is copied inside the /app path of the pod because when testing the image without the -javaagent parameter I could visualize the paths of the pod and the jar file was there.  
Could you help me?  
thanks in advance

---

<div class="post-metadata">

**Author:** ![Sylvain\_Juge](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sylvain_juge/32/55521_2.png) [@Sylvain\_Juge](https://discuss.elastic.co/u/Sylvain_Juge)\
**Post date:** [January 19, 2023, 10:04am UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/2 "2023-01-19T10:04:24Z")

</div>

Hi,

It looks like that the JVM tries to interpret this `-javaagent` argument as a class name, which is weird, could you try moving this argument within the `ENV JAVA_OPTS` above ?  
Also, could you try to use an absolute path for the java agent ? The JVM error message seems to indicate it tries to load a file named `.app.elastic-apm-agent-1.19.0.jar` with an extra `.` at the begining.

As a side note the 1.19.0 version is very old and you should use a more recent agent version.  
In [Manual setup with -javaagent flag | APM Java Agent Reference [master] | Elastic](https://www.elastic.co/guide/en/apm/agent/java/master/setup-javaagent.html#setup-javaagent-get-agent) (in the `Docker` tab), you can get the latest agent with a single `COPY` statement in your `Dockerfile`.

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [January 19, 2023, 12:05pm UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/3 "2023-01-19T12:05:49Z")

</div>

I think the '–' in "–javaagent..." is not the expected hyphen character '-'. Typo correction should fix it

---

<div class="post-metadata">

**Author:** ![irivas95](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@irivas95](https://discuss.elastic.co/u/irivas95)\
**Post date:** [January 19, 2023, 12:42pm UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/4 "2023-01-19T12:42:33Z")

</div>

thanks for your replies  
I have solved it by putting all CMD arguments in one line.  
now my problem is another one, I get the following error in the apm server:  
"message": "service unavailable", "service.name": "apm-server", "url.original":"/config/v1/agents", "http.request.method": "POST", "user\_agent.original": "apm-agent-java/1.30.0 (opbeans-java-k8s 0.0.1-SNAPSHOT)", "source.address": "XX.XXX. X.X","event.duration":16959180,"http.response. status\_code":503,"error.message":"{"statusCode":403,"error":"Forbidden","message":"Forbidden"}","ecs.version":"1.6.0"}

---

<div class="post-metadata">

**Author:** ![Jack\_Shirazi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jack_shirazi/32/91641_2.png) [@Jack\_Shirazi](https://discuss.elastic.co/u/Jack_Shirazi)\
**Post date:** [January 19, 2023, 2:39pm UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/5 "2023-01-19T14:39:29Z")

</div>

Check you can curl the apm server, this looks like you have missing credentials? Also please use the latest Java agent (1.35.0) rather than an ancient one, as we can't really spend time checking issues that may be fixed

---

<div class="post-metadata">

**Author:** ![irivas95](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@irivas95](https://discuss.elastic.co/u/irivas95)\
**Post date:** [January 25, 2023, 9:45am UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/6 "2023-01-25T09:45:57Z")

</div>

I corrected my problem by another way,  
Thanks

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 15, 2023, 5:46am UTC](https://discuss.elastic.co/t/apm-java-agent-could-not-find-or-load-main-class/323484/7 "2023-02-15T05:46:08Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
