Java 编程方式获取与类关联的二进制数据

发布于 2024-12-17 05:24:06 字数 239 浏览 2 评论 0原文

有哪些方法可以在运行时获取与所有类关联的二进制数据,即 java 字节码(我想将数据写入磁盘)。

而且我不想使用 JMX。

基本上,我试图检测类的问题,并且我想将正在运行的字节码中的类放在磁盘上。

  1. 是否有从类加载器运行的所有类的列表?

  2. 我如何获得该列表?

  3. 获得类列表后,我可以使用什么代码来提取字节码数据?

What are some ways to get the binary data associated with ALL classes at runtime, the java bytecode (I want to write the data to disk).

And I don't want to use JMX.

Basically, I am trying to detect an issue with a class and I want to put the class on disk from the running bytecode.

  1. Is there a list of ALL classes that are running from the classloader?

  2. How do I get that list?

  3. What code can I use to extract the bytecode data once I get a list of classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗地喜欢 2024-12-24 05:24:06

我有一种感觉,您应该能够通过实现自己的类加载器来做到这一点,该类加载器记录加载的类并在加载它们时捕获它们的字节码文件。但事后这样做会困难得多。

除非您尝试逆向工程/破解加密的应用程序(tsk,tsk),否则可能有更好的方法来“检测问题”。您为什么不告诉我们您真正想要做什么?


如果您只需要加载的类的列表,请使用 -verbose:class 选项集启动 JVM。

I have a feeling that you should be able to do this by implementing your own class loader that records the classes that are loaded and captures their bytecode files as they are being loaded. But doing this after the fact would be much harder.

Unless you are trying to reverse engineer / crack an encrypted application (tsk, tsk) there is probably a better way to "detect the issue". Why don't you tell us what you are really trying to do?


If you just want a list of the classes that are loaded, launch the JVM with the -verbose:class option set.

痴梦一场 2024-12-24 05:24:06

就像Stephen C所说的那样,-verbose选项可以列出一个列表来跟踪加载的类。
如果你想提取类文件,也许你可以使用java.lang.instrument来提取类文件。它只是用于检测 java 字节代码。

基本方式如下:

Instrumentation最大的作用,是动态变化的定义和操作。开发人员可以在普通Java程序(具有Java的主要功能)运行时通过-javaagent参数指定特定的jar文件(包括Instrumentation Agents)来启动Instrumentation代理。
摘要说的是以下步骤:

  • 准备好 premain 函数

    编写一个Java类,其中包含以下两个方法中的任意一个
    public static void premain (String agentArgs, Instrumentation inst), [1] public static void premain (String agentArgs), [2]

    package example.verboseclass;

公共类主要{
公共静态无效 premain(String args, Instrumentation inst) {
...
}
其中,[1 ]

的优先级比[2]的优先级高将会被执行([1]和[2]同时被法院忽略[2])。
在这个premain函数中,开发者可以进行该类型的操作。
AgentArgs是premain函数给进程的参数,伴随着“-javaagent”一起来的。和main函数不同的是,这个参数是一个字符串而不是一个字符串数组,如果程序的参数有多个,程序就会自行解析字符串。
Inst是一个java.lang.instrument.Instrumentation实例,从JVM自动导入。 java.lang.instrument.Instrumentation 仪器包是一个接口的定义,是这个包的核心部分,它集中了几乎所有的功能,例如类型转换和操作的定义等等。您还必须实现此接口

包sample.verboseclass;

public class Main {

public static void premain(String args, Instrumentation inst) {
    inst.addTransformer(new Transformer());
}

}

class Transformer Implements ClassFileTransformer {

public byte[] transform(ClassLoader l, String className, Class<?> c,
        ProtectionDomain pd, byte[] b) throws IllegalClassFormatException {
    System.out.print("Loading class: ");
    System.out.println(className);
    return b;
}

}

  • Jar 文件打包

    这个Java类将被打包到一个jar文件中,并且通过将“Premain-Class”添加到通过premain Java类准备的指定步骤中来显示属性。 (可能还需要指定其他属性来开启更多功能)

    清单版本:1.0
    Premain-Class:sample.verboseclass.Main

  • 操作

    通过以下 Instrumentation 与 Java 程序一起运行:
    java-javaagent: jar 文档位置 [= 导入的 premain 参数]

就像在你的项目中一样,你可以在ClassFileTransformer方法中编写代码来提取字节码,这将通过字节码加载器加载到其中。

Just like what Stephen C said , the -verbose option can make out a list trace the classes loaded.
If you want to extract the class files ,maybe you can use the java.lang.instrument to get the class files out. It's just for instrumenting java byte code.

The basic way is as follows:

Instrumentation the biggest role, is the definition of dynamic change and operation. Developers can an ordinary Java programs (with the main function of Java) running through-javaagent parameters specify a specific jar files (including Instrumentation Agents) to start the agent Instrumentation .
Summary says is the following steps:

  • Prepared premain function

    Preparation of a Java class, which contains the following two methods of any one
    Public static void premain (String agentArgs, Instrumentation inst), [1] public static void premain (String agentArgs), [2]

    package sample.verboseclass;

public class Main {
public static void premain(String args, Instrumentation inst) {
...
}
}

Among them, the priority [1] [2] level than high priority will be the implementation of ([1] and [2] At the same time, the court has been neglected [2]).
In this premain function, a developer can carry out the type of operation.
AgentArgs is premain function to the process parameters, accompanying "-javaagent" came together. And the main function is different, this parameter is a string rather than a string array, if the parameters of a number of procedures, procedures that will be self-analytical string.
Inst is a java.lang.instrument.Instrumentation examples from JVM automatically imported. Java.lang.instrument.Instrumentation instrument package is the definition of an interface, is the core part of this package, which concentrated almost all of the features, for example, the definition of type conversion and operation, and so on. You must as well implement this interface

package sample.verboseclass;

public class Main {

public static void premain(String args, Instrumentation inst) {
    inst.addTransformer(new Transformer());
}

}

class Transformer implements ClassFileTransformer {

public byte[] transform(ClassLoader l, String className, Class<?> c,
        ProtectionDomain pd, byte[] b) throws IllegalClassFormatException {
    System.out.print("Loading class: ");
    System.out.println(className);
    return b;
}

}

  • Jar file packing

    This Java class will be packaged into a jar file, and in which the properties are manifest by adding "Premain-Class" to a specified steps that are prepared with premain the Java class. (May also need to specify other properties to open more features)

    Manifest-Version: 1.0
    Premain-Class: sample.verboseclass.Main

  • Operation

    Running by the following Instrumentation with the Java programs:
    Java-javaagent: jar document position [= imported premain parameters]

As in your project , you can write codes to extract byte code in the in ClassFileTransformer method , which will pass the byecode loader loading into it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文