Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming in Java bytecode [closed]

Tags:

java

bytecode

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!

like image 772
Corey Stevens Avatar asked Jun 30 '10 14:06

Corey Stevens


People also ask

What happens to Java bytecode?

Bytecode is the compiled format for Java programs. Once a Java program has been converted to bytecode, it can be transferred across a network and executed by Java Virtual Machine (JVM). Bytecode files generally have a .

What is bytecode in Java?

What Is the Bytecode? Bytecode is the intermediate representation of a Java program, allowing a JVM to translate a program into machine-level assembly instructions. When a Java program is compiled, bytecode is generated in the form of a . class file.

What is Java bytecode explain with an example?

An example of bytecode One of the most common examples of bytecode in action is the Java programming language. When an application is written in Java, the Java compiler converts the source code to bytecode, outputting the bytecode to a CLASS file.

How does Java bytecode look like?

class files consist of a bunch of bytecodes. Bytecode is to Java what assembler is to C++. Each bytecode is a number no larger than a byte and has a mnemonic. The numbers and their mnemonic are what you have listed in your question.


2 Answers

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?

like image 200
Adam Paynter Avatar answered Sep 22 '22 12:09

Adam Paynter


I've created a new Java bytecode assembler that is backwards compatible with Jasmin but also adds lots of new features and simplifies the syntax slightly.

Here's an example of how you might write a Hello World program.

.class public hello .super java/lang/Object  .method public static main : ([Ljava/lang/String;)V     .limit stack 10     .limit locals 10      getstatic java/lang/System out Ljava/io/PrintStream;     ldc "Hello World!"     invokevirtual java/io/PrintStream println (Ljava/lang/Object;)V     return .end method 

I've also written a tutorial on bytecode assembly. It currently only covers Hello, World, but I can continue it if there is interest.

like image 38
Antimony Avatar answered Sep 21 '22 12:09

Antimony