Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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.

Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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
METHOD mainAT ENTRYIF trueDO 
traceln("entering into main method")
ENDRULE   
RULE trace main exitCLASS BytemanDemonstration
METHOD mainAT EXITIF trueDO 
traceln("exiting the main method")
ENDRULE


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.

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
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 }}