Berlin Brown
Berlin Brown

Reputation: 11734

Java programmatic ways to get the binary data associated with a class

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?

Upvotes: 1

Views: 816

Answers (2)

biaobiaoqi
biaobiaoqi

Reputation: 1098

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718758

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.

Upvotes: 2

Related Questions