I am trying to understand how .class files work in java and what's their purpose. I found some information online, but I get unsatisfying explanations.

As soon as we run the compiler we get the .class file, which is bytecode. Is this machine readable or not? And if not, this is why we need the interpreter for the program to run successfully?

Also, since the .class file is the equivalent of our .java programs, why can't somebody run a java program straight away by just running the .class file using VM and they would need to have the .java file as well?

有帮助吗?

解决方案

The JVM is by definition a virtual machine, that is a software machine that simulates what a real machine does. Like real machines it has an instruction set (the bytecodes), a virtual computer architecture and an execution model. It is capable of running code written with this virtual instruction set, pretty much like a real machine can run machine code.

So, the class files contain the instructions in the virtual instruction set, and it is capable of running them. For that matter, a virtual machine can either interpret the code itself or compile it for the hardware architecture it is currently running. Some do both, some do just one of them (e.g. .net runtime compiles once the first time the method is called).

For instance, the Java HotSpot initially interprets bytecodes, and progressively compiles the code into machine code. This is called adaptive optimization. Some virtual machines always compile to machine code directly.

So, you can see there are two different "compiling concepts". One consists in the transformation of Java code to JVM bytecodes (From .java to .class). And a second compilation phase happens when the program runs, where the bytecodes may either be interpreted or compiled to actual machine code. This is done by the just-in-time compiler, within the JVM.

So, as you can see, a computer cannot run a Java program directly because the program is not written in a language that the computer understands. It is written in lingua-franca that all JVM implementations can understand. And there are implementations of that JVM for many operating systems and hardware architectures. These JVMs translate the programs in this lingua-franca (bytecodes) for any particular hardware (machine code). That's the beauty of the virtual machine.

其他提示

  1. The .class file is machine-readable. The machine that reads it is the Java Virtual Machine, which interprets it and compiles it to native code (executable by your computer).

  2. You don't need the .java files to run Java code. The .class files are all you need.

It's machine readable, but does not execute on the bare hardware. It's run through the Java Virtual Machine which is an interpreter with a very high performance just-in time compiler. There are good reasons to have the interpreter only use the class file's bytecode. Briefly they are:

  1. Easier to build the interpeter since the bytecode is much closer to instructions that can be turned into native machine code by the JIT.
  2. Easier to resolve dependencies since the Java compiler does some syntactic sugar on them through the import command.

Java bytecode (.class file) is not directly executable. It's an intermediate language that is interpreted by the underlying Java Virtual Machine. Of course some optimizations can happen (i.e. Just-in-time compilation).


To run a Java program you only need the bytecode files, .java files contains the source code.

Compiler Vs Interpreter:

  1. Compiler Takes an entire program as input
    Interpreter Takes Single instructions as input.

  2. Intermediate Object Code is Generated
    No Intermediate Object Code is Generated

  3. Conditional Control Statements are Executed faster
    Conditional Control Statements are Executed slower

  4. Memory Requirement: More (Since Object Code is Generated)
    Memory Requirement: Less

  5. Program need not be compiled every time
    Every time higher level program is converted into lower level program

  6. Errorsare displayed after entire program is checked
    Errors are displayed for every instruction interpreted (if any)

  7. Example: C Compiler
    Example: BASIC

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top