0

I would like to create a .class file by myself so I can run it on JVM.

In other words I’d like to write java bytecode directly, but I can’t look at the structure of a class file. I can decompile it and look at the bytecode instructions but what I want to achieve is the reverse process - turning bytecode into .class files.

I read a bit about .class file structure e. g. http://viralpatel.net/blogs/tutorial-java-class-file-format-revealed/ However I am unable to assemble it so I can run a program. I also can’t find a working example to follow so I could create my own file.

I read that Jasmin does something like that, but I would really like to do it directly.

Nartis
  • 19
  • 1
  • 4
  • What do you mean "directly"? You can't write binary files directly. You need some sort of interpreter/compiler/assembler, or a hex editor at the very least. – shmosel Apr 20 '16 at 20:28
  • 1
    why on God sake would you need to do something like that? – ΦXocę 웃 Пepeúpa ツ Apr 20 '16 at 20:29
  • 1
    What's your use case for this? Why do you need to write bytecode directly rather than via a compiler? Also, what code have you tried so far that isn't working for you? – ManoDestra Apr 20 '16 at 20:29
  • They .class file is, literally, the bytecode. Are you asking for some sort of IDE where you can write ASM-like code (using the bytecode instructions) directly? – Zymus Apr 20 '16 at 20:39
  • 1
    In other words you want to inject evil payload in trusted files? – licklake Apr 20 '16 at 20:46
  • Apparently, you are assuming wrong meanings when using the terms “structure of a class file”, “decompile”, “bytecode instructions” or “bytecode”, and “.class files”. As you have written, the question is a big contradicting nonsense, requiring a lot of re-interpretation by the reader. Besides that, you don’t need Blogs which “reveal” the class file format, there is [an official specification](https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html) which makes the primary source. – Holger Apr 25 '16 at 16:42

3 Answers3

4

The question is whether you want to write custom bytecode in a human friendly format, or if you want to literally write a binary classfile by hand.

If you want to write a binary file by hand, you obviously need a Hex Editor. I've written classfiles directly in a hex editor before, and it is a very annoying and error prone process, and it is not really feasible for anything more complicated than Hello World.

Luckily, I've written an assembler which allows you to write bytecode in a human friendly textual form and assemble it to binary classfiles. Unlike other Java bytecode assemblers, Krakatau gives you very low level control over the results and is designed to support the full classfile format, as well as some undocumented features that the JVM accepts in practice.

This allows you to do pretty much anything you might want to, while Krakatau automatically handles the low level file format details and binary encoding for you. It's as close as you can get to directly writing a classfile, short of doing it by hand in a hex editor.

Here is an example of an assembly file for a Hello World program using the v0 Krakatau syntax.

.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
Antimony
  • 37,781
  • 10
  • 100
  • 107
1

Unless you're writing this weeks new language that runs on the JVM, I'm not sure why you'd want to do this but the class file format docs are for you.

stdunbar
  • 16,263
  • 11
  • 31
  • 53
1

You're probably better off using an existing library like ASM which will do the work for you. This allows you to generate the bytecode without actually writing out the binary format directly.

Unrelated but there's someone walking through building a new language on top of the bytecode format here. This may date over time but might be worth reading through for a starting point.

AlBlue
  • 23,254
  • 14
  • 71
  • 91