Apm java agent could not find or load main class

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

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

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 (in the Docker tab), you can get the latest agent with a single COPY statement in your Dockerfile.

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

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"}

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

I corrected my problem by another way,
Thanks

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