Chapter 3. Hello World Example

Table of Contents

Hello World Imports
Hello World Creation
Hello World Get
Hello World Print

Here is a simple Java program which will print "Hello World" along with the hostname of the system it is run on.

/*
 * Simple "hello world" style SysInfo example.
 */
package example1;

import com.magnicomp.sysinfo.v2.SysInfo;
import com.magnicomp.sysinfo.v2.core.SysInfoCore2;
import com.magnicomp.sysinfo.common.SysInfoException;

public class HelloWorld {
	public static void main(String[] args) {
		SysInfoCore2 sysInfo = null;

		try {
			// Create SysInfo object
			SysInfo si = new SysInfo();
			// Get the SysInfo object
			sysInfo = si.get();
		} catch (SysInfoException e) {
			System.err.printf("SysInfo Exception: %s", e.traceBack());
			System.exit(1);
		}

		System.out.printf("Hello World, my HostName is %s\n",
				sysInfo.getGeneral().getHostName().getValue());
	}
}

In the next subsections we will breakdown each component of this code.

Hello World Imports

The import section of the code contains the following:

import com.magnicomp.sysinfo.v2.SysInfo;
import com.magnicomp.sysinfo.v2.core.SysInfoCore2;
import com.magnicomp.sysinfo.common.SysInfoException;

It is important to note the v2 component of the classname packages. The v2 indicates the class is part of the SysInfo Java API version 2. This version is not the same as the SysInfo product release version. The SysInfo Java API version may change over time when significant changes are needed. The API versioning scheme is in place to allow users of this API to minimize the changes necessary when moving from release to release of the SysInfo product.

The class com.magnicomp.sysinfo.common.SysInfoException is SysInfo's primary exception class. It provides traceback methods to allow the API user to determine where a fault occured in SysInfo.