Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3319792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:51:59+00:00 2026-05-17T22:51:59+00:00

I am writing an interpreter in Java for a domain-specific language with some scripting

  • 0

I am writing an interpreter in Java for a domain-specific language with some scripting capabilities. I have already implemented a parser and now need to do a back end. To this end I am considering either to write my own interpreter (either working with abstract syntax trees or with some custom bytecodes) or target JVM (emit and execute Java bytecode at runtime).

Could someone with more experience in this area say how feasible is the approach of targeting JVM and what libraries would you recommend to use for emitting Java bytecode?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:52:00+00:00Added an answer on May 17, 2026 at 10:52 pm

    Here is a working “hello world” made with ObjectWeb ASM (a library which I recommend):

    package hello;
    
    import java.lang.reflect.Method;
    
    import org.objectweb.asm.ClassWriter;
    import org.objectweb.asm.Label;
    import org.objectweb.asm.MethodVisitor;
    import org.objectweb.asm.Opcodes;
    
    public class HelloWorldASM implements Opcodes {
        public static byte[] compile(String name) {
            ClassWriter cw = new ClassWriter(0);
            MethodVisitor mv;
    
            cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "hello/HelloWorld", null,
                    "java/lang/Object", null);
    
            cw.visitSource("HelloWorld.java", null);
    
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                Label l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(4, l0);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
                        "()V");
                mv.visitInsn(RETURN);
                Label l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLocalVariable("this", "Lhello/HelloWorld;", null, l0, l1,
                        0);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            {
                mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main",
                        "([Ljava/lang/String;)V", null, null);
                mv.visitCode();
                Label l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(7, l0);
                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                        "Ljava/io/PrintStream;");
                mv.visitLdcInsn(String.format("Hello, %s!", name));
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
                        "(Ljava/lang/String;)V");
                Label l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLineNumber(8, l1);
                mv.visitInsn(RETURN);
                Label l2 = new Label();
                mv.visitLabel(l2);
                mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l2,
                        0);
                mv.visitMaxs(2, 1);
                mv.visitEnd();
            }
            cw.visitEnd();
    
            return cw.toByteArray();
        }
    
        public static class DynamicClassLoader extends ClassLoader {
            public Class<?> define(String className, byte[] bytecode) {
                return super.defineClass(className, bytecode, 0, bytecode.length);
            }
        };
    
        public static void main(String[] args) throws Exception {
            DynamicClassLoader loader = new DynamicClassLoader();
            Class<?> helloWorldClass = loader.define("hello.HelloWorld",
                    compile("Test"));
            Method method = helloWorldClass.getMethod("main", String[].class);
            method.invoke(null, (Object) new String[] {});
        }
    }
    

    To generate the code, I found very useful Bytecode Outline for Eclipse plug-in. Although you could use the ASMifier (included with ASM) like this:

    ClassReader cr = new ClassReader(new FileInputStream("HelloWorld.class"));
    cr.accept(new ASMifierClassVisitor(new PrintWriter(System.out)), 0);
    

    At runtime, if you need to obtain the Class object for the created class, you can load your class by extending a class loader and publishing (through another method, for instance) the defineClass method and providing the class as a byte array, as listed in the example.

    You can also handle the created class with an interface, like in this example:

    package hello;
    
    import org.objectweb.asm.ClassWriter;
    import org.objectweb.asm.Label;
    import org.objectweb.asm.MethodVisitor;
    import org.objectweb.asm.Opcodes;
    
    public class HelloWorldPlugin implements Opcodes {
        public static interface Plugin {
            void sayHello(String name);
        }
    
        public static byte[] compile() {
    
            ClassWriter cw = new ClassWriter(0);
            MethodVisitor mv;
    
            cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "hello/MyClass", null,
                    "java/lang/Object",
                    new String[] { "hello/HelloWorldPlugin$Plugin" });
    
            cw.visitInnerClass("hello/HelloWorldPlugin$Plugin",
                    "hello/HelloWorldPlugin", "Plugin", ACC_PUBLIC + ACC_STATIC
                            + ACC_ABSTRACT + ACC_INTERFACE);
    
            {
                mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
                mv.visitCode();
                Label l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(5, l0);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>",
                        "()V");
                mv.visitInsn(RETURN);
                Label l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLocalVariable("this", "Lhello/MyClass;", null, l0, l1, 0);
                mv.visitMaxs(1, 1);
                mv.visitEnd();
            }
            {
                mv = cw.visitMethod(ACC_PUBLIC, "sayHello",
                        "(Ljava/lang/String;)V", null, null);
                mv.visitCode();
                Label l0 = new Label();
                mv.visitLabel(l0);
                mv.visitLineNumber(9, l0);
                mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out",
                        "Ljava/io/PrintStream;");
                mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
                mv.visitInsn(DUP);
                mv.visitLdcInsn("Hello, ");
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder",
                        "<init>", "(Ljava/lang/String;)V");
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder",
                        "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder",
                        "toString", "()Ljava/lang/String;");
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println",
                        "(Ljava/lang/String;)V");
                Label l1 = new Label();
                mv.visitLabel(l1);
                mv.visitLineNumber(10, l1);
                mv.visitInsn(RETURN);
                Label l2 = new Label();
                mv.visitLabel(l2);
                mv.visitLocalVariable("this", "Lhello/MyClass;", null, l0, l2, 0);
                mv.visitLocalVariable("name", "Ljava/lang/String;", null, l0, l2,
                        1);
                mv.visitMaxs(4, 2);
                mv.visitEnd();
            }
            cw.visitEnd();
    
            return cw.toByteArray();
        }
    
        public static class DynamicClassLoader extends ClassLoader {
            public DynamicClassLoader(ClassLoader parent) {
                super(parent);
            }
    
            public Class<?> define(String className, byte[] bytecode) {
                return super.defineClass(className, bytecode, 0, bytecode.length);
            }
        };
    
        public static void main(String[] args) throws Exception {
            DynamicClassLoader loader = new DynamicClassLoader(Thread
                    .currentThread().getContextClassLoader());
            Class<?> helloWorldClass = loader.define("hello.MyClass", compile());
            Plugin plugin = (Plugin) helloWorldClass.newInstance();
            plugin.sayHello("Test");
        }
    }
    

    Have fun.

    PS: I can add comments to the code if not clear enough. I didn’t because the answer is already too long. Nevertheless, my suggestion for you is to try debugging it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an interpreter for an experimental language. Three of the main constructs of
I've heard of the idea of bootstrapping a language, that is, writing a compiler/interpreter
I'm writing a command line interpreter and I'm trying to setup formats for individual
It seems to me that if you are writing in an interpreted language that
Writing some test scripts in IronPython, I want to verify whether a window is
Writing the code for the user authentication portion of a web site (including account
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing my first Linq application, and I'm trying to find the best way to
When writing a switch statement, there appears to be two limitations on what you

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.