How to Make and Run Java Programs in Linux

Java is a universal programming language. Java is used for writing simple applications to enterprise scale applications. Since Java can be written once and run anywhere, it has become the choice for cross platform programming. If you are new to programming languages, especially to Java, this article explains how to create your first program in Java and run it in a Linux system.

First of all, you need a few things before starting with your first Java program on Linux. You need to download and install JDK 1.6 from Sun’s Java website. If you are running a Linux system such as Ubuntu, you can use Ubuntu’s package manager to download JDK and install it for you. Once downloaded and installed, you need to make sure that JKD works fine. For that, go to the terminal and execute the following command.

#java –version

This will give you the Java version installed in the system.

Next, you need a text editor in order to write your first Java code. Since there are plenty of text editors available for Linux, you can choose the one you are most familiar with.

Let’s now move on to creating your first application. Your first application will write “Hello World” on the screen when the application is executed.

First of all, we need to create a Java source file with your program. By writing your Java code in a source file, the Java compiler can understand what you have written, so it can compile your program easily. In addition, the other Java programmers also can understand your Java program in case if they are supposed to enhance or maintain what you wrote. You can use any kind of text editor for writing and editing your Java program. When you go to advance levels of Java, you can simply use more advanced tools with syntax highlighting. For now, let’s stick with a basic text editor.

It is always a good idea to have a separate directory for your Java programs. For that, you can create a directory and move there through the terminal. In order to do that, execute the following commands in terminal.

#mkdir myapps

#cd myapps

Then ‘myapps’ directory will be created in your home directory.

Open up your favorite text editor and write the following code in there.

class MyClass {

public static void main(String[] args) {

System.out.println(“Hello World!”);



Now save your file with the name ‘’ in ‘myapps’ directory. Make sure that you save the file with .java extension. Otherwise, your file will not be compiled.

Now it is time for us to compile the Java source file. Go to the ‘myapps’ directory (you are already there) through the terminal and execute the following command to make sure you are in the right place.


This command will show you’re your current path. Then you can execute the following command to see your .java file is available in the current directory.


If you see your .java source file available, you can go ahead and start compiling your source file by executing;


Please remember the rule of thumb. The source filename always should be as same as the class name in the source file. Javac is the Java compiler which compiles your source file to an executable binary file. Once javac is successfully executed, you will see that there is another file named ‘MyClass.class’.

Once you have a .class file, you can execute your program by executing;

#java MyClass

The above command just includes the class name and no extension is required.

Upon the execution of the above command, you will see following in the terminal.

Hello World!

Congratulations! This is your first Java program in Linux.

Author: Nilanka

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.