You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Anand Raman <ar...@sapient.com> on 2003/04/08 10:22:16 UTC

Newbie problems | HelloWorld

Hi guys

I am trying to create a very simple class which outputs "hello world"

This is the chunk of code which I have written.

<snip>

public void testCreateSimpleFile() {
    ClassGen cg = null;
    ConstantPoolGen cpg = null;
    InstructionList il = null;
    MethodGen mg = null;
    InstructionFactory factory = null;
    
    cg = new ClassGen("HelloWorld", 
                    "java.lang.Object", "<generated>", Constants.ACC_PUBLIC|Constants.ACC_SUPER, null);
    cpg = cg.getConstantPool();

    factory = new InstructionFactory(cg);
    il = factory.createPrintln("Hello World !!");
            
    mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_STATIC, Type.VOID,
                                    new Type[] { new ArrayType(Type.STRING, 1) },
                                    new String[] { "args" },
                                    "main",
                                    "HelloWorld",
                                    il, cpg);
    mg.setMaxStack();
    cg.addMethod(mg.getMethod());
    il.dispose();
    
    try {
        cg.getJavaClass().dump("HelloWorld.class");
    } catch(IOException ioe) {
        System.err.println(ioe);
    }
}
</snip>

On running the class I get a VerifyError 
Exception in thread "main" java.lang.VerifyError: (class: HelloWorld, method: main signature: ([Ljav
a/lang/String;)V) Falling off the end of the code

What could be the reason.. 

I am pretty new to jvm byte code concepts so any direction would help. 

Also as a add on question, what does the learning curve look like for bcel. Are there any resources present which can help in learning BCEL easily. Any pointers

Thanks for your time.
anand

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: Newbie problems | HelloWorld

Posted by Stephen Kolaroff <ch...@riflexo.com>.
I had simmilar problems in the beginning; so will answere how, IMHO a 
newbie can make its life easier:)
1. JDK verifier is usualy not quite verbose - use Justice instead. 
Justice is available in the classpath:
org.apache.bcel.verifier:
    org.apache.bcel.verifier.Verifier.main(new String[]{<<your classes>>});
2. Read the JVM spec. There you may find, for example, that every path 
of execution in a method must be terminated with appropriate RETURN 
instruction. So you will need smth like
il.append(InstructionConstants.RETURN);
(take a look at InstructionConstants; you will find many useful thinks 
there - it is better to use ready-made RETURN statement than to create 
new RETURN() every time you need one).
3. Read this list's archives; many people had posted anounces about 
their codegen facilities based on BCEL. Those may help.

And many others
Regards
Cheffo

Anand Raman wrote:

>Hi guys
>
>I am trying to create a very simple class which outputs "hello world"
>
>This is the chunk of code which I have written.
>
><snip>
>
>public void testCreateSimpleFile() {
>    ClassGen cg = null;
>    ConstantPoolGen cpg = null;
>    InstructionList il = null;
>    MethodGen mg = null;
>    InstructionFactory factory = null;
>    
>    cg = new ClassGen("HelloWorld", 
>                    "java.lang.Object", "<generated>", Constants.ACC_PUBLIC|Constants.ACC_SUPER, null);
>    cpg = cg.getConstantPool();
>
>    factory = new InstructionFactory(cg);
>    il = factory.createPrintln("Hello World !!");
>            
>    mg = new MethodGen( Constants.ACC_PUBLIC | Constants.ACC_STATIC, Type.VOID,
>                                    new Type[] { new ArrayType(Type.STRING, 1) },
>                                    new String[] { "args" },
>                                    "main",
>                                    "HelloWorld",
>                                    il, cpg);
>    mg.setMaxStack();
>    cg.addMethod(mg.getMethod());
>    il.dispose();
>    
>    try {
>        cg.getJavaClass().dump("HelloWorld.class");
>    } catch(IOException ioe) {
>        System.err.println(ioe);
>    }
>}
></snip>
>
>On running the class I get a VerifyError 
>Exception in thread "main" java.lang.VerifyError: (class: HelloWorld, method: main signature: ([Ljav
>a/lang/String;)V) Falling off the end of the code
>
>What could be the reason.. 
>
>I am pretty new to jvm byte code concepts so any direction would help. 
>
>Also as a add on question, what does the learning curve look like for bcel. Are there any resources present which can help in learning BCEL easily. Any pointers
>
>Thanks for your time.
>anand
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: bcel-user-help@jakarta.apache.org
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org