Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify java agent inside 'fat jar'?

I am using the jetty-alpn-agent to add ALPN support to my project, but all I can find is instructions on how to run it from the .m2 folder, which makes me need to deploy two jar:s instead of just my one uber-jar, making it less portable.

Is is possible to specify a location inside the jar to the -javaagent switch?

I'm looking for something like java -javaagent:my.jar!/javaagents/jetty-alpn-agent-2.0.0.jar -jar myjar.jar, but that doesn't seem to work.

like image 349
Niklas Herder Avatar asked Aug 19 '16 07:08

Niklas Herder


People also ask

How do I specify Javaagent?

To pass the -javaagent argument on WebSphere: From the admin console, select Servers > Application servers > (select a server) > Configuration > Service Infrastructure > Java and Process Management. Select Process Definition > Additional Properties, then select Java Virtual Machine. Select Apply, then select Save.

What is Java agent in JVM?

Java agents are a special type of class which, by using the Java Instrumentation API, can intercept applications running on the JVM, modifying their bytecode. Java agents aren't a new piece of technology. On the contrary, they've existed since Java 5.

Can you have multiple Java agents?

You can configure a separate Java Agent installation for each process (JVM) on a host.

How spring BOOT fat jar works?

In spring boot applications the concept of fat jar is mostly used . The spring boot fat jar file has all the artifacts bundled together as jars so that it can be easily deployed and shipped . All the external and internal dependencies along with application level properties are bundled in one single jar.


1 Answers

It might be possible according to the java.lang.instrumentation documentation.

If the implementation allows it the jetty-alpn-agent.jar must be part of the system classpath. So you must include it in your my.jar like any other application library.

Starting Agents After VM Startup

An implementation may provide a mechanism to start agents sometime after the the VM has started. The details as to how this is initiated are implementation specific but typically the application has already started and its main method has already been invoked. In cases where an implementation supports the starting of agents after the VM has started the following applies:

The manifest of the agent JAR must contain the attribute Agent-Class. The value of this attribute is the name of the agent class.

The agent class must implement a public static agentmain method.

The system class loader ( ClassLoader.getSystemClassLoader) must support a mechanism to add an agent JAR file to the system class path.

The agent JAR is appended to the system class path. This is the class loader that typically loads the class containing the application main method. The agent class is loaded and the JVM attempts to invoke the agentmain method. The JVM first attempts to invoke the following method on the agent class:

public static void agentmain(String agentArgs, Instrumentation inst);

If the agent class does not implement this method then the JVM will attempt to invoke:

public static void agentmain(String agentArgs);

The agent class may also have an premain method for use when the agent is started using a command-line option. When the agent is started after VM startup the premain method is not invoked.

The agent is passed its agent options via the agentArgs parameter. The agent options are passed as a single string, any additional parsing should be performed by the agent itself.

The agentmain method should do any necessary initialization required to start the agent. When startup is complete the method should return. If the agent cannot be started (for example, because the agent class cannot be loaded, or because the agent class does not have a conformant agentmain method), the JVM will not abort. If the agentmain method throws an uncaught exception it will be ignored.

PS: I have never tried this. Please let me know if it works or not.

like image 86
René Link Avatar answered Oct 07 '22 06:10

René Link