How to run a Java program in Ubuntu

sachin@sachin-Lenovo-G550:~$ java -version
java version “1.7.0_21″
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK Server VM (build 23.7-b01, mixed mode)

When I ran the above command this showed that Java is installed in my system, but when I am going to compile any Java program it gives the following error message:

sachin@sachin-Lenovo-G550:~/programs$ javac abc.java
The program ‘javac’ can be found in the following packages:
* default-jdk
* ecj
* gcj-4.6-jdk
* gcj-4.7-jdk
* openjdk-7-jdk
* openjdk-6-jdk
Try: sudo apt-get install

Please tell me how to get rid of it and run my Java program.


Open the terminal and run:

sudo apt-get install openjdk-7-jdk

and then compile your Java program as before with: javac abc.java. Then run it with:

java abc  ## The name of the class to be called is abc NOT abc.class

You can also substitute openjdk-6-jdk instead of openjdk-7-jdk in the first command. In Ubuntu 15.10 and newer, you can also substitute openjdk-8-jdk instead of openjdk-7-jdk in the first command. In Ubuntu 17.10 you can also substitute openjdk-9-jdk. In Ubuntu 17.10 and later you can also substitute openjdk-11-jdk.

In Java 9 and letter, Java has a built-in shell that can run blocks of Java code directly from the terminal without compiling the Java code first, jshell, defined in JEP 222. To start jshell from the terminal type jshell.

$ jshell
|  Welcome to JShell -- Version 11.0.7
|  For an introduction type: /help intro

jshell>

To exit from jshell type /exit.


If you prefer to install Oracle JDK, a step by step instruction on installing Oracle JDK 8 is explained in this article : Install Latest Oracle JDK in Ubuntu

Step 1: Download the latest JDK(jdk-Xuxx-linux-xXX.tar.gz) from this official link.

Step 2: Open the terminal (Ctrl + Alt + T) and enter the following command.

sudo mkdir /usr/lib/jvm

Step 3: Enter the following command to change the directory.

cd /usr/lib/jvm

Step 4: Extract the jdk-Xuxx-linux-xXX.tar.gz file in that directory using this command.

sudo tar -xvzf ~/Downloads/jdk-8u45-linux-x64.tar.gz

Step 5: Enter the following command to open the environment variables file.

sudo gedit /etc/environment

Step 6: In the opened file, add the following bin folders to the existing PATH variable.

/usr/lib/jvm/jdk1.8.0_45/bin
/usr/lib/jvm/jdk1.8.0_45/db/bin
/usr/lib/jvm/jdk1.8.0_45/jre/bin

The PATH variables have to be separated by semicolon. Notice that the installed JDK version is 1.8 update 45. Depending on your JDK version, the paths can be different. Add the following environment variables at the end of the file.

J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"

The environment file before the modification:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

The environment file after the modification:

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/jvm/jdk1.8.0_45/bin:/usr/lib/jvm/jdk1.8.0_45/db/bin:/usr/lib/jvm/jdk1.8.0_45/jre/bin"
J2SDKDIR="/usr/lib/jvm/jdk1.8.0_45"
J2REDIR="/usr/lib/jvm/jdk1.8.0_45/jre"
JAVA_HOME="/usr/lib/jvm/jdk1.8.0_45"
DERBY_HOME="/usr/lib/jvm/jdk1.8.0_45/db"