0

I disassembled a java class file to Assembly using javap. Then Can I run the assembly code generated by the javap command without Os? Or is there something like il2cpu.net for java?

6
  • What do you mean, exactly, by "without OS"? Commented Apr 6, 2022 at 14:28
  • Like running the generated code directly in a processor
    – scardon
    Commented Apr 6, 2022 at 14:28
  • Can the generated code run directly on a processor?
    – scardon
    Commented Apr 6, 2022 at 14:33
  • 1
    I guess by "without OS" the OP really means "without a virtual machine". Well, the answer is no. Commented Apr 6, 2022 at 14:39
  • 1
    You're confused, @JFan - bytecode and machinecode are not the same thing at all. java isn't compiled to 'assembly', it's compiled to bytecode which no chip can run. a JVM can run this (there used to be a chip something like 20 years ago, but it wasn't as simple as 'it will just run bytecode', and isn't around anymore). Commented Apr 6, 2022 at 14:43

2 Answers 2

4

No you can't.

  1. The javap command outputs disassembled bytecodes, not machine instructions.

  2. You can't run disassembled code. Disassembled code is just text .. that needs to be assembled to ... something ... before it can be executed ... by something.

  3. Bytecodes cannot be executed by regular hardware1. On regular hardware (e.g. Intel, M1, SPARC, etc) they need to be interpreted / JIT compiled by a regular JVM, or compiled to native code using an AOT compiler.

  4. All mainstream JVMs run on top of an operating system. Even outliers like JNode (which did boot and run on bare x86 hardware) have much of the functionality of an operating system embedded in them.

  5. If you compiled the bytecodes to native code executable, you would still need an operating system to run the executable on. Just like you would any other native code executable.


1 - It is possible in theory to implement hardware that executes bytecodes as its native instruction set, but no such hardware is commercially available. There was a Sun project to build a native Java platform, but it was canned a long time ago. There are others too, but nothing has gained traction. See https://en.wikipedia.org/wiki/Java_processor for some leads. Also https://en.wikipedia.org/wiki/Jazelle but that apparently requires a "software JVM" to handle complicated things, and ... it has been "de-emphasized" in ARMv7.

8
  • Then can I run a java program directly on a processor?
    – scardon
    Commented Apr 6, 2022 at 14:40
  • The Java virtual machine, in all likelihood, compiles your Java to native code that runs directly on the CPU (e. g. is not interpreted). This is call JIT (just-in-time) compilation, most JVMs support that. Commented Apr 6, 2022 at 14:42
  • @JFan - No. There is no practical way to do that. (Apart from JNode ... which is a dead project now, and never really worked well enough to be viable. You really need an OS underneath to deal with stuff like memory management, etc. JNode on a micro-kernel OS might have been viable, but they had other ideas.)
    – Stephen C
    Commented Apr 6, 2022 at 14:43
  • @Stephen C: read up re:Jazelle. ARM is in the middle of phasing it out, though. Commented Apr 6, 2022 at 14:44
  • I hadn't heard of that one before. But in a sense it does prove my point. It requires special hardware. And even then, the Java on Jazelle runtime will rely on the platform OS for all of the things that an OS normally does.
    – Stephen C
    Commented Apr 6, 2022 at 14:55
3

No :)

  • javap does not generate code. It can print bytecode and the fields+methods, but it is not a decompiler.
  • You can run bytecode in a JVM *) (which runs on a OS), but for that you don't need javap
  • You also tranform a class/jar to native-image and run without JVM, but you will still need an OS. See here

*) strictly speaking, there is also hardware capable of running bytecode directly: https://en.wikipedia.org/wiki/Java_processor

5
  • 2
    but it is not a decompiler. - Also doesn't produce native machine code or a CPU assembly language representation of it (like x86 add eax, [rdi]). From a .class file, you need a (JIT or AoT) compiler to get machine code. CPUs don't run source-code or Java bytecode, they run machine instructions (format depends on the ISA, that's why we have JVMs). Ah, I see StephenC's answer already covers this. Commented Apr 6, 2022 at 22:32
  • 1
    Technically, decompling is the process of reversing a compile step. Compiling does not necessarily have to compile to machine-code, but can also produce intermediate-level-byte-code (java byte-code, but also for example LLVM IR) that needs further processing to be able to be run on a CPU. Commented Apr 8, 2022 at 15:16
  • 1
    Well sure, but if it was a decompiler, you'd have Java source code, back to square 1, since you can't run that a bare CPU either (especially not directly; it needs to be in some kind of binary format for it to even be plausible). Commented Apr 8, 2022 at 15:21
  • So is there something like il2cpu.net for java
    – scardon
    Commented Apr 10, 2022 at 6:46
  • 1
    @ArshCoder dead link, but I'm guessing that was a site that compiled .NET IL (or some other intermediate language bytecode) into native machine instructions for some ISA, presumably x86. Using the standard .NET runtime's optimizing JIT compiler. To do the same thing for Java, you run your .class files with JVM options to dump the JIT-optimized native machine code (or text disassembly of it.) Commented Jun 14 at 4:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.