I'm new in JavaAgent concept, so maybe this issue isn't complicated. But I hope some discussion and knowledge arrangement will be helpful (not only for me).
So - what I want to do? I would like to load external jar library when I start my application. This external library is under development and versioned on ARM (Nexus) and contains functionalities which are essential in my_app.
My attempt:
Download wanted version of lib (no problem here - got proper func_version.jar in right place)
I add in parent dir of downloaded jar class JavaAgent.java:
import java.lang.instrument.*; public class JavaAgent { private static Instrumentation inst; public static Instrumentation getInstrumentation() { return inst; } public static void premain(String agentArgs, Instrumentation inst) { System.out.println(inst.getClass() + ": " + inst); System.out.println("JavaAgent LOADED!"); JavaAgent.inst = inst; } }
Modify
META-INF/MANIFEST.MF
in lib (added line:Premain-Class: JavaAgent
)Try to load JavaAgent by passing it as JVM parameter:
java -javaagent:/home/func_1.0.1.jar /home/my_app/MyStartup
MyStartup.java
contains main method for my_app.
I know this steps are not enough (can't see any result yet). But is it a right approach? And what else do I need to do to see wanted "JavaAgent LOADED!" on console output? Some work with Java ClassLoader is needed?