Search code examples
javaaspectjjavaagents

Check if aspectjweaver (or any javaagent) is loaded


Is there a (pref portable) way to check if The JVM has been stated with a particular -javaagent?

In particular I'm interested to know if the aspectj load time weaver has loaded or not. (I'm trying to provide a helpful error msg in the case of incorrect startup).


Solution

  • The following code shows

    • a way to determine any -javaagent:... JVM arguments,
    • a way to check if the AspectJ weaving agent entry point class (the one mentioned in the manifest entry Premain-Class: of aspectjweaver.jar) is loaded.

    The former just proves that the argument was given on the command line, not that the agent was actually found and started.

    The latter just proves that the weaver is available on the classpath, not that it was actually started as an agent. The combination of both should give you pretty much confidence that the agent is actually active.

    package de.scrum_master.app;
    
    import java.lang.management.ManagementFactory;
    import java.lang.management.RuntimeMXBean;
    import java.util.List;
    
    public class Application {
        public static void main(String[] args) {
            RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
            List<String> arguments = runtimeMxBean.getInputArguments();
            for (String argument : arguments) {
                if (argument.startsWith("-javaagent:"))
                    System.out.println(argument);
            }
            try {
                Class.forName("org.aspectj.weaver.loadtime.Agent");
            } catch (ClassNotFoundException e) {
                System.err.println("WARNING: AspectJ weaving agent not loaded");
            }
        }
    }
    

    You also might find the question Starting a Java agent after program start and some of its answers helpful.


    Update:

    Okay, here is a combination of my own solution and yours, but one which actually works even if the weaver is unavailable, which is important because this is what you want to check in the first place:

    public static boolean isAspectJAgentLoaded() {
        try {
            Class<?> agentClass = Class.forName("org.aspectj.weaver.loadtime.Agent");
            Method method = agentClass.getMethod("getInstrumentation");
            method.invoke(null);
        } catch (Exception e) {
            //System.out.println(e);
            return false;
        }
        return true;
    }
    

    Update 2:

    After some discussion with the OP bacar I have decided to offer a solution which does not use reflection but catches NoClassDefError instead:

    public static boolean isAspectJAgentLoaded() {
        try {
            org.aspectj.weaver.loadtime.Agent.getInstrumentation();
        } catch (NoClassDefFoundError | UnsupportedOperationException e) {
            System.out.println(e);
            return false;
        }
        return true;
    }
    

    Now both main error types

    • weaving agent is available on the classpath, but instrumentation has not been initiated because aspectjweaver.jar was not started as a Java agent,
    • agent aspectjweaver.jar is not on the classpath at all and class org.aspectj.weaver.loadtime.Agent is thus unavailable

    are handled gracefully by returning false after warning messages (in this simple examples just the exceptions which say clearly what is wrong) have been printed on the console.

    Possible console outputs for the two cases are:

    • java.lang.UnsupportedOperationException: Java 5 was not started with preMain -javaagent for AspectJ
    • java.lang.NoClassDefFoundError: org/aspectj/weaver/loadtime/Agent