什么是 premain() 以及如何调用它? [英] What is premain() and how does it get called?

查看:129
本文介绍了什么是 premain() 以及如何调用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从未听说过 premain 并且我觉得问起来有点愚蠢,但是 这篇文章的答案 建议运行它以获得 Instrumentation 对象.

I have never heard of a premain and I feel a little stupid to ask but the answer of this post suggests to run it in order to get the Instrumentation object.

但是如何调用该函数或如何使其被调用?

But how does that function get called or how do I make it getting called?

package playground;
import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }
}

推荐答案

premain 是一种与 java.lang.instrument 包关联的机制,用于加载代理"在 Java 程序中进行字节码更改.

The premain is a mechanism associated with the java.lang.instrument package, used for loading "Agents" which make byte-code changes in Java programs.

机制在中有说明java.lang.instrument 文档.

The mechanism is explained in the java.lang.instrument documentation.

它的要点是代理"部署在一个 jar 中,并且该 jar 在其清单中有一个特殊条目,它告诉检测包在哪里查找 premain 方法.您引用的来源应该是一个简单的代理.

The gist of it is that the "agent" is deployed in a jar, and that jar has a special entry in its manifest, that tells the instrumentation package where to look for the premain method. The source you quoted is supposed to be a simple agent.

这篇关于什么是 premain() 以及如何调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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