This blog teaches you how to compile and run java program in Linux terminal. We will write a very simple program which will print random numbers. To write Java program we will use vim Linux editor. Let us see step by by step instructions.

how to compile and run java program in Linux terminal ( Steps )
  • Open vim editor in Linux terminal and write sample code and save it as RandomNum.java

import java.util.Random;

class RandomNum {
  public static void main(String[] args) {

    Random r = new Random();

    for (int i = 1; i <= 5; i++) {
      System.out.println(r.nextInt(5));
    }
  }
}
  • Compile sample java code RandomNum.java

how to compile and run java program in Linux terminal

After the command javac RandomNum.java , a new file would be created named as RandomNum.class. We can see the new executable file by using Linux ls shell command.

bosch@bosch-Inspiron-N5050:~/samplejava$ ls
RandomNum.class  RandomNum.java
  • Run the Java program using command “java RandomNum”

how to compile and run java program in Linux terminal

Ref:




Related Contents to follow