3

I need to add a -javaagent argument in my JVM args when running jar1, but reference jar2 that is contained within jar1. I have tried:

-javaagent:BOOT-INF/lib/jetty-alpn-agent-2.0.0.jar"

with no success. How do I determine the location of JARs within a running system at runtime?

(This is to run jetty-alpn-agent-2.0.0.jar as a Java Agent for HTTP/2 use with Pushy APNs)

MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106

2 Answers2

0

If you know that the class is on the class path, you can typically do:

URL jar = MainClass.class.getProtectionDomain().getCodeSource().getLocation();

The Javaagent is always loaded on the class path which is why you should be able to reference the main class of the regular application.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
0

@indusBull

The solution that worked for me was to define a property called libDirectory in my gradle.properties file, which was the location of where the JARs are located in a running system, and then reference that variable from my Dockerfile:

gradle.properties

libDirectory = /opt/meanwhileinhell/app/lib

Dockerfile

ENTRYPOINT  ["java",\
            ...
            ...
            "-javaagent:${libDirectory}/jetty-alpn-agent.jar",\
            "-jar", "/app.jar"]
MeanwhileInHell
  • 6,780
  • 17
  • 57
  • 106