How to Compile and Run a Simple Java Program

11th February 2019 0 By Alin Bistrian

Before we look into what is JAVA_HOME and why do we need it I believe it is beneficiary to understand how classes are compiled and run from the terminal.

Note that if you used a java installer, java and javac commands may work even if you do not add the path to the JDK bin directory because the installer would have added it for you. This tutorial will explain how it works and how to use the JDKs compiler and how to run the compiled classes with the same JDK.

 

JDK Refresher

If you remember from the introduction to JDK that if you want to develop Java applications, you need a JDK because you need to compile the .java files into .class files. And that if you’re going to run Java applications, a JRE (Java Runtime Environment) is enough. And here is how the diagram looked like:

*Note: This is the Mac OS X version. You require a mac and to have a JDK installed to be able to follow this tutorial. My current installed JDK is jdk1.8.0_171.jdk

If you do not have a JDK installed, then you can find instructions on how to install it on your Mac OS X here: https://www.focusonjava.com/how-to-install-java-development-kit-mac-os-x/  (Windows example will come soon).

If you already have a JDK installed then you can find it under /Library/Java/JavaVirtualMachines/jdk…/ like in the screenshot:

Compiling Java classes with javac command

Before we dive into how to how to compile a java class from the command line, we need to create a class and put it into a .java file.

Open a text editor of your choice and type the following class into it:

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

Then save this class into MyClass.java file under a directory of your choice, mine is /Users/<user>/Projects/MyProject/MyClass.java

If you prefer to use the terminal here is a way to do that.

 


user@comp$ ls
user@comp$ MyClass.java

 

Now we are ready to compile and run our simple Java program, but first, we need to understand how JDK will compile our class.

The JDK will compile our java classes using the javac executable file like as follows:

 


user@comp$ javac MyClass.java

 

But there is a problem with this command. How will the terminal know where the javac executable is located? Well, it does not know unless we will tell it where to find it. You need to see where your JDK is installed, and if you used a .dmg file to install it then most probably it will be under/Library/Java/JavaVirtualMachines/… 

 

Here is how mine looks like: /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/javac

 

Once you got the path to javac executable file then navigate to the directory where you have your MyClass.java file and type the following command line, like in the screenshot and hit enter key:

 


user@comp$ /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/javac

 

In the terminal window do an ls, and you should have the following two files:  MyClass.java and MyClass.class


user@comp$ /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/javac

user@comp$ ls

MyClass.class    MyClass.java

 

We have successfully compiled our Java class.

Before we move onto how to run our Java Program, we should mention the two directories that we care about.

Those are JDK /bin and JRE /bin. Why do we care about these directories is because they contain the executable files for java and javac which we use to compile and run our Java Applications.

 

Environment variable PATH

In the example above we have to use the absolute path to the “javac” file to compile our class.

Wouldn’t it be great if we can say:  javac MyClass.java to compile our class?

PATH to the rescue…

We can add the path to the directory where the javac executable file is located to the environment variable PATH.

In the terminal type the following command and hit enter and you should have something like this (but not exactly like mine):

echo $PATH

 


user@comp$ echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS

 

Now let us add the path to javac to the environment variable PATH. In the terminal type the following command and hit enter:

export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin

 


user@comp$ export PATH=$PATH:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin

 

Then type echo $PATH command and hit enter:

 


user@comp$ export $PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin

 

Now that the path to the JDK bin directory is added to the environment variable PATH we can run the javac command without using the full path.

That is because when you say javac, the terminal will check all the paths that are present in the environment variable PATH until will find the path to a directory that contains javac file.

 

Running Java classes using the “java” command

As you can see the JDK compiled our java class which is ready for us to run.

There is also a java executable file in the JDK bin directory which we will use to run the simple java program that we just compiled. Since we already added the JDK bin directory to the PATH, we do not have to type the long path. In the terminal make sure that you are in the directory where the MyClass.class is and type the following command:

java MyClass 

NOTE*:  when you run the “java” command you do NOT need to specify the .class file extension.


user@comp$ java MyClass

Hello World

 

Another way to run the Java Application is by using the JDKs JRE, which also has java executable file in its bin directory but it does not have the javac

 

Here is how it looks like from the terminal using the full path to JREs bin directory to prove that it works. In the terminal type the following command:

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/bin/java MyClass and hit enter.

It should print Hello World like in the screenshot:

 


user@comp$ java MyClass

Hello World

/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/jre/bin/java MyClass

Hello World

 

NOTE*: You can also have an independent JRE installation, but since we need to develop Java Applications, we are going to use the JDK to run those apps.