1

I need to add an instrumentation JAR to my Java command line. If possible, I want to this argument to reference a JAR included in my Maven dependencies. As an example:

java -javaagent:tooling.jar -jar myapplication.jar

This "tooling.jar" does not exist as an explicit file in the filesystem. Instead, it is listed as a runtime dependency in Maven:

<dependency>
   <groupId>com.foo.bar</groupId>
   <artifactId>tooling</artifactId>
   <scope>runtime</scope>
</dependency>

If it's relevant, I'm using Java 11 and Spring Boot.

The purpose of this is to avoid having to move two separate JAR files to every run environment (for technical reasons I am only able to deploy the application JAR). As far as I can tell there's nothing technically wrong with this approach, as the JAR does exist - it is just wrapped into the application JAR. However, nowhere that I've found has described this process, so I wonder if I'm doing something terribly wrong. I haven't been able to test this yet. Is this a valid pattern and what is the appropriate syntax for it?

WannabeCoder
  • 498
  • 1
  • 5
  • 20
  • You're trying to unpack an uber jar? – Stewart Oct 10 '22 at 20:43
  • @Stewart I'm not sure what you mean. All I'm trying to do is reference a packaged dependency (the instrumentation JAR tooling.jar) that is inside the application JAR (packaged by Maven). It's going to take me a while to set up a test case, so I wanted to see if what I was doing was possible and/or recommended. The lack of information on the subject is usually an indicator that the solution is obvious and I'm missing something. – WannabeCoder Oct 10 '22 at 20:49
  • Does your packaging tool preserve the fully qualified class names, ie, the package names, etc? If so ... does it work to try `java -javaagent:myapplication.jar -jar myapplication.jar` – Stewart Oct 10 '22 at 20:58
  • 1
    I am unsure how Maven handles the build process regarding class names. I did manage to find a possible-duplicate that indicates what I want is impossible: https://stackoverflow.com/questions/27424364/specifying-a-javaagent-within-the-jar-file-to-be-run – WannabeCoder Oct 10 '22 at 22:01
  • How about some feedback to my answer? I think it is rather impolite to ask for help in public and then keep your helpers waiting. – kriegaex Nov 13 '22 at 09:24

1 Answers1

0

You can attach a Java agent either via -javaagent:/my/own/agent.jar like you already do or during runtime. But in both cases, the agent has to be a regular file on a file system. Agents cannot be loaded any other way.

Therefore, either you need to deploy the agent to your target execution environment separately, or you need to bootstrap by extracting it manually from your uber JAR during application start-up and then hot-attach it before loading any of the classes you wish to instrument.

kriegaex
  • 63,017
  • 15
  • 111
  • 202