How Java Programs Run

Eclipse takes care of a lot of it for you.  But what if we weren’t using Eclipse?  Could we still create and run Java files?  Yes.  

High-level Languages

Java is a high-level language that allows coders to write programs that can be ran independent of the computer they are using. Other high-level languages you may have heard of include Python, C++, PHP, Ruby, and JavaScript.

Programs in high-level languages have to be translated into a low-level language called “machine language”. High-level languages are shorter and easier to read; therefore, it is much easier to write in a high-level language. They can also run on different kinds of computers with few or no modifications. Low-level programs can run on only one kind of computer.

Two kinds of programs translate high-level languages into low-level languages: interpreters and compilers. 

An interpreter reads a high-level program and executes it, meaning that it does what the program says in the order it says it.

A compiler reads the entire program and translates it completely before the program starts running. 

Source Code and Object Code

Source code is the organized code of the programming language that humans can read. All high-level languages utilize source code for humans to give instructions in. Once a program is compiled, it is translated into object code. At that point, the code can be executed repeatedly without further translation of the source code. As a result, compiled programs run faster than interpreted programs.  Humans cannot read or understand the object code or the executable.  Those files are in machine language.  In Java, files that end with .java contain the human readable code while .class files contain the machine language code.

Note that machine language code is not portable across different devices. You cannot run an executable compiled for a Windows machine on a Mac. Source code would need to be compiled multiple times on multiple machines in order to run a program on different types of machines.

How Java works on all devices

In order to allow for maximum usage across all devices, Java is both compiled and interpreted. Instead of translating source code directly into an executable, the Java compiler generates code for a virtual machine. This “imaginary” machine has the functionality common to all computing devices. The object code, called Java byte code, is easy and fast to interpret using the virtual machine.

As a result, it’s possible to compile a Java program on one machine, transfer the byte code to another machine, and run the byte code on that other machine.  The Java compiler is a program named javac. It translates .java files into .class files that store the resulting byte code. The Java interpreter is another program, named java, which is short for “Java Virtual Machine” (JVM).

If a Java programmer was not using an IDE, the task of writing source code and running the Java byte code is more complex. Hypothetically, the programmer would write source code in a text file and save it as HelloPrinter.java.  Next, they use javac to compile it through a command line. If there are no errors, the compiler saves the byte code in the file HelloPrinter.class. To run the program, the programmer again uses java to interpret the byte code in the command line. The result of the program displays on the command line screen.

Eclipse and other development environments automate these steps for you. You only have to press a button to run your Java program.  Eclipse will automatically save, compile and run the Java byte code within the IDE.   

Up Next: Debugging Programs