I'm looking to write a short program (maybe a Hello World) in Java bytecode. I just want to write the bytecode using my text editor and run it. How would I do this? Got an example? Thanks!
typo: it's BCEL. Otherwise great editor
HH
2010-07-01 08:25:27
+1
A:
Maybe this article can get you started: Bytecode basics (a little old, but you will get the idea).
The class file format will come in handy too :D
dpb
2010-06-30 14:39:45
+10
A:
You could try Jasmin!
.class public HelloWorld
.super java/lang/Object
.method public static main([Ljava/lang/String;)V
.limit stack 3
.limit locals 1
getstatic java/lang/System/out Ljava/io/PrintStream;
ldc "Hello World."
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
return
.end method
You compile it using:
> java -jar jasmin.jar hello.j
And then you run it like any class:
> java HelloWorld Hello World.
Update
I see that your question mentions "without using Javac or Java". Could you clarify how you meant that statement?
Adam Paynter
2010-06-30 14:43:30
This post makes me want to fake the work I'm doing today and tinker around with Jasmin. :-)
glowcoder
2010-06-30 14:47:19
+1 Jasmin is what came to my mind and I couldn't remember the name. It was featured in a book that explains JVM internals. I Forget the name of the book too, *sigh*...
Bakkal
2010-06-30 14:48:10
@Bakkal: According to the link: "Jasmin was originally created as a companion to the book "Java Virtual Machine", written by Jon Meyer and Troy Downing and published by O'Reilly Associates."
Adam Paynter
2010-06-30 14:50:26
by "without using Javac or Java," I just meant that I want to write the code using bytecode. Thanks for the info!
Corey Stevens
2010-06-30 15:06:53
wow~ This is super cool. is this JVM independent as well? can this run on a blackberry jvm?
Viele
2010-06-30 15:36:07
@Viele: Yes, this should be JVM independent (so long as the JVM you're interested in conforms to the JVM specification).
Adam Paynter
2010-06-30 16:28:09
@Corey, how is this different from using a plan `.java` file and `javac` ?
OscarRyz
2010-06-30 17:42:14
Wow!! I've been working for a while on bytecode. Why haven't I heard of this?
HH
2010-07-01 08:24:46
+1
A:
Byte code is written as actual bytes, which are not normally easily editable by a normal text editor.
This means you will need something that converts a textual representation to binary. A reasonable place to start would be an assembler like Jasmin. http://en.wikipedia.org/wiki/Jasmin_(Java_assembler)
Thorbjørn Ravn Andersen
2010-06-30 14:44:36