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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值