Skip to content

Instantly share code, notes, and snippets.

@jeffrade
Created June 6, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffrade/f2ca62da8e49e18e8a375d2a67a56304 to your computer and use it in GitHub Desktop.
Save jeffrade/f2ca62da8e49e18e8a375d2a67a56304 to your computer and use it in GitHub Desktop.
How to create a `javaagent` executable jar
import java.lang.instrument.Instrumentation;
/**
* Let's say you have an existing application that is an executable jar file (e.g. app.jar)
* Define this class a directory of your choosing.
* Then create the following manifest.txt file:
* <code> $ echo "Premain-Class: ExampleAgent" > manifest.txt</code>
* Then create the jar file for this class:
* <code> $ jar cmf manifest.txt example-agent.jar *.class</code>
* Lastly, start your existing app.jar like so:
* <code> $ java -javaagent:/full/path/to/example-agent.jar -jar app.jar</code>
*/
class ExampleAgent {
public static void premain(String args, Instrumentation inst) {
System.out.println("################################ in premain");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment