Jasm

Assembler / Disassembler for Java Bytecode

View JavaDoc API View on GitHub

About

Jasm is an Assembler / Disassembler for Java Bytecode. Using Jasm you can easily read or write Java Classfiles. Jasm was originally developed as part of the Java Compiler Kit, and is now used primarily within the Whiley Compiler.

Example: Reading an Existing Class

Here's some example code using Jasm to read in an existing class, and print its name:

try {
     FileInputStream fin = new FileInputStream("test.class"); 
     ClassFileReader reader = new ClassFileReader(fin);
     ClassFile cf = reader.readClass();
    
     System.out.println("Read class called: " + cf.name());
} catch(IOException e) {
     System.err.println("Error - " + e.getMessage());
}	

You can find the complete example here.

Example: Creating a New Class

Here's some example code using Jasm to create a class and write it to a file:


List modifiers = new ArrayList();
modifiers.add(Modifier.ACC_PUBLIC);

ClassFile cf = new ClassFile(
     49,                                 // Java 1.5 or later
     new JvmType.Clazz("","Example"),    // class is Example
     JvmTypes.JAVA_LANG_OBJECT,          // superclass is Object
     Collections.EMPTY_LIST,             // implements no interfaces
     modifiers);                         // is public

try {
     FileOutputStream fos = new FileOutputStream("Example.class");
     ClassFileWriter cfw = new ClassFileWriter(fos);
     cfw.write(cf);
} catch(IOException e) {
     System.err.println("Error - " + e.getMessage());
}

You can find the complete example here.

You can also find another example here showing how Jasm can be used to generate a "Hello World" class.

Authors and Contributors

The Jasm Assembler / Disassembler is developed by David J. Pearce (@DavePearce)