检查是否加载了aspectjweaver(或任何javaagent) [英] Check if aspectjweaver (or any javaagent) is loaded

查看:974
本文介绍了检查是否加载了aspectjweaver(或任何javaagent)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有(pref便携式)方式来检查
JVM是否已使用特定 -javaagent 声明?

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

特别是我有兴趣知道是否加载了aspectj加载时间织入器。 (我试图在启动不正确的情况下提供有用的错误消息)。

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

推荐答案

以下代码显示


  • 一种确定任何 -javaagent的方法:... JVM参数,

  • 一种检查AspectJ编织代理入口点类( aspectjweaver的清单条目 Premain-Class:中提到的类)的方法.jar )已加载。

  • 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");
        }
    }
}

您也可能会发现问题在程序启动后启动Java代理,其中一些答案很有帮助。

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

更新:

好的,这是我自己的解决方案和你的解决方案的组合,但是即使编织器不可用也能正常工作,这很重要,因为这是你想要首先检查的内容:

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






更新2 :

在与OP bacar 进行一些讨论之后,我决定提供一个不使用反射但是捕获<$的解决方案c $ c> NoClassDefError 而是:

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

现在两种主要错误类型


  • 编织代理在类路径上可用,但尚未启动检测,因为 aspectjweaver.jar 未作为Java代理启动,

  • 代理 aspectjweaver.jar 根本不在类路径上,因此类 org.aspectj.weaver.loadtime.Agent 不可用

  • 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

通过在之后返回 false 来优雅地处理控制台上已经打印了警告信息(在这个简单的例子中只是明确说明错误的例外情况)。

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未使用preJain -javaagent为AspectJ启动

  • java.lang.NoClassDefFoundError:org / aspectj / weaver / loadtime / Agent

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

这篇关于检查是否加载了aspectjweaver(或任何javaagent)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆