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

  • Home
  • SEARCH
  • 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 3808022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:04:58+00:00 2026-05-19T15:04:58+00:00

I write a simple program using Java ASM to generate the byte code which

  • 0

I write a simple program using Java ASM to generate the byte code which is generated by compiling the following class.

public class Main {
    public static void main(String[] args) {
        System.out.println("Test");
    }
}

The code I wrote to generate the bytecode for this class is given below.

public class CodeGenerator {

    public void generateClass()
    {
        ClassWriter cw=new ClassWriter(Opcodes.NULL);
        FieldVisitor fv;
        MethodVisitor mv;
        cw.visit(Opcodes.V1_1, Opcodes.ACC_PUBLIC, "Main", null, "java/lang/Object", null);
        mv=cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "()V", null, null);
        mv.visitVarInsn(Opcodes.AALOAD, 0);
        mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(1, 1);
        mv.visitEnd();
        mv=cw.visitMethod(Opcodes.ACC_PUBLIC+ Opcodes.ACC_STATIC, "Main", "([Ljava/lang/String;)V", null, null);
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream");
        mv.visitLdcInsn("Test");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        mv.visitInsn(Opcodes.RETURN);
        mv.visitMaxs(2, 1);
        mv.visitEnd();
        cw.visitEnd();
        this.WriteClass(cw);
    }
    public void WriteClass(ClassWriter cw){
        FileOutputStream fos;
        try{
            fos = new FileOutputStream("E:\\Acadamic\\Final year project\\ASM\\Main.class");
            fos.write(cw.toByteArray());
            fos.close();
        }
        catch (IOException ex){
            Logger.getLogger(CodeGenerator.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

This compiles successfully and when I try to run it, it gives the following error.

Exception in thread "main" java.lang.NoClassDefFoundError: main/class
Caused by: java.lang.ClassNotFoundException: main.class
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: main.class.  Program will exit.

can any one please help me for avoid this error. If anyone can explain the cause to the error and how to avoid it, please be kind enough to help me.

Thank You.

Follow-up:

When I run it like java Main it gives the following error.

Exception in thread "main" java.lang.ClassFormatError: Field "out" in class Main
 has illegal signature "Ljava/io/PrintStream"
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Main.  Program will exit.
 
  • 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-19T15:04:59+00:00Added an answer on May 19, 2026 at 3:04 pm

    I don’t think your error is being caused by ASM. It looks like you simply called java incorrectly. You should call it like this:

    java Main

    My guess is that you called it like this:

    java Main.class

    Update

    There are three problems with your code.

    Problem #1

    The ClassWriter constructor accepts a bit mask of options. You passed it Opcodes.NULL. Although this may seem correct, you actually want to pass it 0 (Opcodes.NULL means something different).

    ClassWriter cw=new ClassWriter(0);
    

    Problem #2

    You are calling AALOAD in the constructor instead of ALOAD. The AALOAD instruction loads an array element, whereas ALOAD loads a local variable.

    mv.visitVarInsn(Opcodes.ALOAD, 0);
    

    Problem #3

    In your Main method, you forgot the semicolon after Ljava/io/PrintStream:

    mv.visitFieldInsn(
        Opcodes.GETSTATIC,
        "java/lang/System",
        "out",
        "Ljava/io/PrintStream;" // Notice the semicolon
      );
    

    You can save yourself some pain by taking advantage of ASM’s Type class:

    mv.visitFieldInsn(
        Opcodes.GETSTATIC,
        "java/lang/System",
        "out",
        Type.getObjectType("java/io/PrintStream").getDescriptor()
      );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple program in VC++ which will just initialize the
I'm just starting out writing trying to write a simple program in C and
I am trying to write simple Visual Studio Add-In for code generation. In my
I need to write a simple command-line application in Java. It would be nice
I wrote a simple Windows Forms program in C#. I want to be able
I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I want to write a simple chat-client in Ruby for the terminal. The Problem
I try to write a simple client/server application (all application is a bluetooth service
I try to write a simple Markdown parser in JavaScript. Therefore I want to
I wrote a sample program at http://codepad.org/ko8vVCDF that uses a template function. How do

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.