Clashsoft
Clashsoft

Reputation: 11882

Specify -javaagent Option in Manifest

To tell the JVM to invoke a Java Instrumentation agent before the main class of a Jar file, you usually have to invoke it with a command option:

java -javaagent:agent.jar program.jar

Having to type this out every time is pretty inconvenient, so is there a way to specify the agent in the program.jar manifest?

# program.jar/META-INF/MANIFEST.MF
...
Java-Agent: agent.jar

Upvotes: 5

Views: 2510

Answers (2)

Alan Bateman
Alan Bateman

Reputation: 5449

Java 9 adds the Launcher-Agent-Class attribute which can be used on executable JAR files to start an agent before the main class is loaded.

Upvotes: 5

Rafael Winterhalter
Rafael Winterhalter

Reputation: 44032

Unfortunately, there is no such option. As an alternative, you can use the attach API to attach a Java agent dynamically. This does however have some limitations, e.g. it typically only works on a JVM that is distributed with a JDK.

For a convenient API for attaching agents, have a look at Byte Buddy Agent.

Upvotes: 1

Related Questions