java agentor,检查是否加载了aspectjweaver(或任何javaagent)

java agentor,检查是否加载了aspectjweaver(或任何javaagent)Istherea(prefportable)waytocheckifTheJVMhasbeenstatedwithaparticular-javaagent?InparticularI’minterestedtoknowiftheaspectjloadtimeweaverhasloadedornot.(I’mtryingtop…

java agentor,检查是否加载了aspectjweaver(或任何javaagent)

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).

解决方案

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 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/36090.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注