Installing Oracle Java on Ubuntu 14.04 LTS

You will need to run the following as the root user. To install Java 7…

add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install oracle-java7-installer

…and for Java 8…

add-apt-repository ppa:webupd8team/java
apt-get update
apt-get install oracle-java8-installer

You will have to agree and accept the following licenses:

Oracle Java 8 - Accept License

Oracle Java 8 - Accept Binary License

You can quickly test both java and javac with the following….

java -version
javac -version

…or you may want to check out one of my previous posts here.

Related Documents

https://salayhin.wordpress.com/2014/04/23/install-oracle-java-78-jdk-and-jre-in-ubuntu-14-04/

https://www.digitalocean.com/community/tutorials/how-to-install-java-on-ubuntu-with-apt-get

http://askubuntu.com/questions/521145/how-to-install-oracle-java-on-ubuntu-14-04

Using HelloWorld to Test a JDK Installation

To test that your Java Development Kit (JDK) is installed correctly, you need to write a simple Java program, compile it, and run the compiled bytecode.

[andy@home-pc ~]$ mkdir ~/java-demos
[andy@home-pc ~]$ cd ~/java-demos
[andy@home-pc java-demos]$ vim HelloWorld.java

A simple Java HelloWorld example looks like this.

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("\nThe Java Development Kit (JDK) is installed and working!!\n");
    }
}

You compile Java code with the javac command, and then run the resulting file with the java command.

$ ls
HelloWorld.java
$ javac HelloWorld.java
$ ls
HelloWorld.class
HelloWorld.java
$ java HelloWorld

The Java Development Kit (JDK) is installed and working!!

$ 

You should note that although the resulting Java bytecode is a file called HelloWorld.class, you run the program without any file extension.

You could take this a step further and write some code that returns some Java system properties about the current installation.

$ ls
HelloWorld.class
HelloWorld.java
$ cp -v HelloWorld.java JDKInstallationTest.java

The below code makes use of the getProperties() method, of the System import java.util.Map; class JDKInstallationTest { public static void main(String[] args) { System.out.println("\nThe " + System.getProperty("java.vendor") + " Java version " + System.getProperty("java.version") + " is installed on this system.\n"); // Print a list of Java properties System.out.println("Java Properties:"); for(Map.Entry e : System.getProperties().entrySet()) { if (((String)e.getKey()).startsWith("java")) { System.out.println(e); } } System.out.println(); } } Again, run it like so. [bash] $ ls HelloWorld.class HelloWorld.java JDKInstallationTest.java $ javac JDKInstallationTest.java $ ls HelloWorld.class HelloWorld.java JDKInstallationTest.class JDKInstallationTest.java $ java JDKInstallationTest [/bash] The output will look something like this.

The Oracle Corporation Java version 1.8.0_31 is installed on this system.

Java Properties:
java.runtime.name=Java(TM) SE Runtime Environment
java.vm.version=25.31-b07
java.vm.vendor=Oracle Corporation
java.vendor.url=http://java.oracle.com/
java.vm.name=Java HotSpot(TM) 64-Bit Server VM
java.vm.specification.name=Java Virtual Machine Specification
java.runtime.version=1.8.0_31-b13
java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment
java.endorsed.dirs=/usr/lib/jvm/java-8-jdk/jre/lib/endorsed
java.io.tmpdir=/tmp
java.vm.specification.vendor=Oracle Corporation
java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.specification.name=Java Platform API Specification
java.class.version=52.0
java.awt.printerjob=sun.print.PSPrinterJob
java.specification.version=1.8
java.class.path=.
java.vm.specification.version=1.8
java.home=/usr/lib/jvm/java-8-jdk/jre
java.specification.vendor=Oracle Corporation
java.vm.info=mixed mode
java.version=1.8.0_31
java.ext.dirs=/usr/lib/jvm/java-8-jdk/jre/lib/ext:/usr/java/packages/lib/ext
java.vendor=Oracle Corporation
java.vendor.url.bug=http://bugreport.sun.com/bugreport/

$ 
You can then delete the .class files if you wish, leaving only the source (.java) files.

$ ls
HelloWorld.class
HelloWorld.java
JDKInstallationTest.class
JDKInstallationTest.java
$ rm -v ./*.class
removed ‘./HelloWorld.class’
removed ‘./JDKInstallationTest.class’
$ ls
HelloWorld.java
JDKInstallationTest.java
$ 

Related Documents

Lesson: A Closer Look at the “Hello World!” Application

Programmatically getting Version number of Java and Flash using C, C++, or Java