Byteman: Byte Code Manipulation Tool for Logging, Testing, and Fault Injection

DZone 's Guide to

Byteman: Byte Code Manipulation Tool for Logging, Testing, and Fault Injection

Using the Byteman tool to log, test and fault inject your code at the ByteCode level in the JVM. With code examples for quick understanding.

· Java Zone ·
Free Resource

Byteman is a byte code manipulation tool for fault injection, testing, and tracing. The Byteman agent allows you to inject rules into an existing Java application without changing the source code. You can inject the rules during the JVM startup time or into a running application without building and redeploying. We can inject the rules for custom Java classes, private methods, and the JRE libraries as well. Below are some Byteman use cases.

  • Add logging statements for the legacy application.
  • Inject fault scenarios to the application via Junit or TestNG unit test cases.

Now, we will see a simple Java application for which we will add debug statements with Byteman.

Let us write a simple Java application.

package org.smarttechie;   
/*** Class will demonstrate the Byteman* @author Siva**/
public class BytemanDemonstration {
    public static void main(String[] args) {    
        System.out.println("With this class we are demonstrating the byteman"); 

Download the latest Byteman distribution here.

Write a Byteman script to inject the debug statements for the above class.

RULE trace main entryCLASS BytemanDemonstration
traceln("entering into main method")
RULE trace main exitCLASS BytemanDemonstration
traceln("exiting the main method")

Launch the JVM by passing the Byteman agent and the script file path.

java -javaagent:${Byteman_Home}\lib\byteman.jar=script:${SCRIPT_FILE_PATH}\logrules.btm <class_name>

After launching the JVM, you will able to see the log messages coming from the script file.

entering into main method
  With this class 
    we are demonstrating the byteman
    exiting the main method

For further exploration, follow these links.

jvm ,java ,byteman ,byte code ,tutorial

Published at DZone with permission of Siva Prasad Rao Janapati , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}