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</code class.

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.

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

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

Be the first to comment

Leave a Reply