Running JAVA program in Linux Ubuntu

The tutorial bellow explains simple procedure to compile, interpret, and execute Java program in Ubuntu using JDK sofware.

Steps to run Java program in Ubuntu:

1. Go to Applications > Accessories > Terminal, to open terminal window.

2. Type the following in command line

gedit Add.java

3. Enter the code bellow in the editor.

/* Addition of two numbers – Add.java */
import java.io.*;
class Add {
  public static void main(String args[]) {
    InputStreamReader ISR = new InputStreamReader(System.in);
    BufferedReader BR = new BufferedReader(ISR);
    int a = 0, b = 0;
    try {
      System.out.print("Enter the first number: ");
      a = Integer.parseInt(BR.readLine());
      System.out.print("Enter the second number: ");
      b = Integer.parseInt(BR.readLine());
    } catch(Exception e) {
      System.out.println("Check the program.");
    }
    System.out.println("Addition of " + a + " + " + b + " is " + (a+b));
  }
}

4. Save the file and close to return to terminal window.

5. Firstly compile the code using the following command

javac Add.java

That would produce an class file (Add.class - binary file) you may need to run the program.

7. Now run this executable using the following command

java Add

8. Output should show as follows

Enter the first number: 49
Enter the second number: 2
Addition of 49 + 2 is 51

Note: If java is not present in the system, download it through terminal window using command sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk

Comments

Most Popular Posts

Technical Questions

RNA to Protein Translation in PERL

Simple Server and Client Chat using Python