Run Java Program in Ubuntu 18.04

java ubuntu java openjdk

Run Java Program in Ubuntu 18.04. The program will be run using Oracle's OpenJDK JDK 8.

  • Check if Java Runtime Environment(JRE) is installed in machine:

    java -version
    

    If JRE is installed it will show the version information similar to following:

    openjdk 11.0.5 2019-10-15
    OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04)
    OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04, mixed mode)
    

    If you do not have installed JRE, install JRE using:

    sudo apt-get install openjdk-8-jre
    
  • Check if Java compiler is installed in machine:

    javac -version
    

    If Java compiler is installed it will show the version information similar to following:

    javac 1.8.0_232
    

    If you do not have installed Java compiler, install it using:

    sudo apt-get install openjdk-8-jdk
    
  • Go to any directory and create a demo Java program. For example, we are going to create Student.java as following:

    class Student{
    	public static void main(String[] args){
    		System.out.println("Hello from Student class");
    	}
    }
    
  • Compile the Java class using:

    javac Student.java
    
  • Run the compiled program using:

    java Student
    

    You will see the output like below:

    Hello from Student class
    

alt Run Java Program in Ubuntu 18.04

Reference: