import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.GeneratorAdapter;
import org.objectweb.asm.commons.Method;

public class Gen {

    public static void main(String[] args) {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
        cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "GenBCode", null, "java/lang/Object", null);

        Method init = Method.getMethod("void <init>()");
        GeneratorAdapter gaInit = new GeneratorAdapter(Opcodes.ACC_PUBLIC, init, null, null, cw);
        gaInit.visitCode();
        gaInit.loadThis();
        gaInit.invokeConstructor(Type.getType(Object.class), init);
        gaInit.returnValue();
        gaInit.endMethod();

        Method run = Method.getMethod("int run()");
        GeneratorAdapter ga = new GeneratorAdapter((Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC), run, null, null, cw);
        ga.visitCode();
        // BEGIN important part
        ga.push(42);
        // END important part
        ga.returnValue();
        ga.endMethod();

        cw.visitEnd();

        byte[] generatedByteCode = cw.toByteArray();
    }
}

@@ -1,28 +1,112 @@
+import java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A%0A
 import org.objectweb.asm.Cla
@@ -1244,16 +1244,276 @@
 Array();
+%0A%0A        try %7B%0A            Path path = Paths.get(%22GenBCode.class%22);%0A            Files.write(path, generatedByteCode);%0A        %7D catch(Exception e) %7B%0A            System.out.println(%22Cannot write to file%22);%0A        %7D%0A%0A        System.out.println(GenBCode.run());
 %0A    %7D%0A%7D

Write generated class to a .class and run it