I disassembled a java class file to Assembly using javap. Then Can I run the assembly code generated by the javap command without Os?
CodePudding user response:
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 needjavap
- 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
CodePudding user response:
No you can't.
The
javap
command outputs disassembled bytecodes, not machine instructions.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.
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.
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.
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.