How to Run Java Programs in Ubuntu

Running Java programs in Ubuntu

Step 1: Install Java compiler

The simplest way to install JDK on Ubuntu is to go with the default offering from Ubuntu:

sudo apt install default-jdk

Once installed, verify that javac is available now.

javac –version

The Output will come us

javac 11.0.11

finally java compiler is successfully installed.

Step 2: Compile Java program in Linux

You need to have a Java program file for this reason. Let’s say you create a new Java program file named Hello.java and it has the following content:

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

If there is no error, the above command produces no output.

When you compile the Java program, it generates a .class file with the class name you used in your program. You have to run this class file.

Step 3: Run the Java class file

You do not need to specify the class extension here. Just the name of the class. And this time, you use the command java, not javac.

java Hello

This will print Hello Shanthosh on the screen for my program.

Leave a comment