Lesson 2 — Behind The Scenes

A simple intro to Java

Prayag Bhakar
3 min readAug 29, 2016

Previous :: Next

The Compile Process

Here is the basic structure of the process that your computer takes to run your Java programs. We will break all of them down and show an example program.

Java Source Code

This the original file that we type and make. These kinds of files have the file extension .java and are recognized by the computer and will open in your default code editor. The name of this .java file is also the same name of the class name inside of the file. However, for now, we are using an online compiler just to get a fell for the language. When you open up a .java file, the information that is shown can easily be read by humans, as long as they know how to read Java. This file is also known as the source code because this is the code before it is compiled and made into an executable program.

public class MyClass {
public static void main(String args[]) {
System.out.println("Hello Wold");
}
}

Java Compiler

When you run the .java file, it passed onto the Java Compiler. With the online compiler, it compiles the .java file when you press the execute button. However, when we are on the computer, we can run the file using a command line prompt.

javac fileName.java

The compiler’s job is to translate the .java file, which is easily readable by humans, into a .class file, which is easily readable by computers.

Java Byte Code

The .class file keeps the same name as the converted .java files. If you open this file, then you will see Byte Code. As mentioned earlier, it is not human readable, but the computer understands it easily. Then the file is passed to the Java Virtual Machine.

Java Virtual Machine (JVM)

The JVM is essentially a virtual machine running on your computer. Just like a virtual machine running an operating system, this virtual machine has apps and libraries that are called based on the what is written in the .class file. After running the code inside the JVM, the output is shown on your operating system, whether it be Windows, Linux, or OS X. For each operating system, there are different tools to use because each of the operating systems is programmed differently.

note: the name you will see in this part is the class name that your main method is in, not the name of the file it is in

.jar File

When compiling a Java program, another file is generated and that is called the .jar file. This file contains all the classes, that you have coded. For example, if you have a bunch of classes and you needed to send all of them to a friend, you could send the .jar file, and they could unbundle the file, and they would have all of your classes.

Got Questions? Ask them below!

Previous :: Next

--

--