Search code examples
javatomcatmanifestjavassistjavaagents

How to deploy javaagent with 3rd party libs dependencies


We wrote a javaagent to help developers debugging. But, before releasing this tool, we still have some questions about the deployment of java-agent.

User may use the agent with some tomcat applications. The agent uses Premain method to transform classes. We use javassist 3.18.2-GA to insert codes. We currently add javassist.jar into Boot-Class-Path in MANIFEST.MF. And We put both the agent and javassist.jar into tomcat's lib directory.

The questions are:

  1. Well, as least, it works. But is that the correct way to deploy agents and dependencies for tomcat applications?
  2. Because most tomcat applications using hibernate which also using javassist 3.18.2-GA, so it's ok right now. But as my understanding, 3.20 is not completely compatible with 3.18.2. Suggest someone had update the javassist to a higher version, will the agent or the application crash due to the conflict between the 2 different javassist's.

Solution

  • A javaagent is added and run on the VM's class path. Therefore, you have the following options:

    1. Add the dependencies to the classpath when starting up the VM such as you would when deploying a normal application. For an application container like Tomcat, this would be the appropriate directory for such dependencies.
    2. Bundle the dependencies with the agent using a tool like fatjar. Doing so, you can also use the Maven Shade plugin to transfer dependencies into a different namespace to avoid version conflicts.
    3. Use the Instrumentation API to manually append the dependencies before running your actual agent application.