Book Image

Java 9 Programming By Example

By : Peter Verhas
Book Image

Java 9 Programming By Example

By: Peter Verhas

Overview of this book

This book gets you started with essential software development easily and quickly, guiding you through Java’s different facets. By adopting this approach, you can bridge the gap between learning and doing immediately. You will learn the new features of Java 9 quickly and experience a simple and powerful approach to software development. You will be able to use the Java runtime tools, understand the Java environment, and create Java programs. We then cover more simple examples to build your foundation before diving to some complex data structure problems that will solidify your Java 9 skills. With a special focus on modularity and HTTP 2.0, this book will guide you to get employed as a top notch Java developer. By the end of the book, you will have a firm foundation to continue your journey towards becoming a professional Java developer.
Table of Contents (17 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Installing Java


To develop, compile, and execute Java programs, you will need the Java execution environment. As the operating systems that we usually use for software development do not contain the language preinstalled, you will have to download it. Although, there is multiple implementation of the language, I recommend that you download the official version of the software from Oracle. The official site for java is http://java.com and this is the site from where the latest release of the language can be downloaded. At the time of writing this book, the 9th version of Java is not yet released. An early pre-release version is accessible via http://jdk9.java.net/download. Later the release versions will also be available from here.

What you can download from here is a so called early access version of the code that is available only to experiment with it, and no professionals should use it for real professional purposes

On the page, you have to click on the radio button to accept the license. After that, you can click on the link that directly starts the download of the installation kit. The license is a special early access license version that you, as a professional, should carefully read, understand, and accept only if you are agreeable with the terms.

There is a separate installation kit for Windows 32 and 64 bit systems, Mac OS X, Linux 32 and 64 bit versions, Linux for ARM processor, Solaris for SPARC processor systems, and Solaris x86 versions. As it is not likely that you will use Solaris, I will detail the installation procedure only for Windows, Linux, and Mac OS X. In later chapters, the samples will always be Mac OS X, but since Java is a write once, run anywhere language, there is no difference after the installation. The directory separator may be slanted differently, the classpath separator character is a semicolon on Windows instead of a colon, and the look and feel of the Terminal or command application is also different. However, where it is important, I will try not to forget to mention it.

To confuse you, the Java download for each of these operating system versions lists a link for the JRE and one for the JDK. JRE stands for Java Runtime Environment and it contains all the tools and executables that are needed to run Java programs. JDK is the Java Development Kit that contains all the tools and executables that are needed to develop Java programs including the execution of the Java program. In other words, JDK contains its own JRE. For now, all you need to do is download the JDK.

There is one important point of the installation that is the same on each of the three operating systems that you have to be prepared for before the installation: to install Java, you should have administrative privileges.

Installation on Windows

The installation process on Windows starts by double clicking on the downloaded file. It will start the installer that will present you a welcome screen.

Pressing the Next button we get a window where you can select the parts you want to install. Let's leave here the default selection, which means that we install all the downloaded parts of Java and press the button Next. The following window is where we can select the destination folder for the installation.

As for now we do not change the directory selected by the installer. Press Next. Later, when you become a professional Java developer, you may decide to install Java to a different location but then you will already have to know what you are doing.

You may need to click the Next button a few times and then the installer finishes. Provide the administrative password when asked and voila! Java is installed. This is really the very usual Windows installation process.

The last step is to set the environment variable JAVA_HOME. To do that in Windows we have to open the control center and select the Edit environment variables for your account menu.

This will open a new window that we should use to create a new environment variable for the current user.

The name of the new variable has to be JAVA_HOME and the value should point to the installation directory of the JDK.

This value on most of the systems is C:Program FilesJavajdk-9. This is used by many Java programs and tools to locate the Java runtime.

Installation on MAC OS X

In this section, we will take look at how to install Java step-by-step on an OS X platform. I will describe the installation process for the released version available at the time of writing this book. As for now, the Java 9 early access version is a bit tricky to install. It is probable that version Java 9 will have similar or the same install steps as Java 8 update 92 has.

The OS X version of Java comes in the form of a .dmg file. This is a packaging format of OS X. To open it, simply double click on the file in the Download folder where the browser saves it and the operating system will mount the file as a read-only disk image.

There is only one file on this disk: the installation image. Double click on the file name or icon in the Finder application and the installation process will start.

The first screen opening is a welcome screen. Click Continue and you will see the Summary page that displays what will be installed.

It is not a surprise that you will see a standard Java installation. This time, the button is called Install. Click on it and you will see the following:

This is the time when you have to provide the login parameters for the administrative user—a username and password. When provided, installation starts and, in a few seconds, you will see a Summary page.

Click Close and you are ready. You have Java installed on your Mac. Optionally, you can dismount the installation disk and, sometime later, you can also delete the .dmg file. You will not need that, and in case you do, you can download it any time from Oracle.

The last thing is to check whether the installation was okay. Proof of the pudding is eating it. Start a Terminal window and type java -version at the prompt and Java will tell you the version installed.

On the next screenshot you can see the output on my workstation and also the Mac OS commands that are handy to switch between the different versions of Java:

On the screenshot, you can see that I have installed the Java JDK 1.8u92 version and, at the same time, I also have a Java 9 early release installation, which I will use to test the new features of Java for this book.

Installation on Linux

There are several ways to install Java on Linux, depending on its flavor. Here, I will describe an installation method that works more or less the same way on all flavors. The one I used is Debian.

First step is the same as in any other operating system: download the installation kit. In the case of Linux, you should select a package that has a tar.gz ending. This is a compressed archive format. You should also carefully select the package that matches the processor in your machine and the 32/64 bit version of the operating system. After the package is downloaded, you have to switch to root mode, issuing the su command. This the first command you can see on the screenshot that shows the installation commands.

The tar command uncompressed the archive into a subfolder. In Debian, this subfolder has to be moved to /opt/jdk and the mv command is used for this purpose. The two update-alternatives command is Debian-specific. These tell the operating system to use this newly installed Java in case there is already an older Java installed. The Debian I was using to test and demonstrate the installation process on a virtual machine came with a 7 year old version of Java.

The final step of the installation is the same as any other operating system: checking that the installation was successful in issuing the java -version command. In the case of Linux, this is even more important because the installation process does not check that the downloaded version matches the operating system and the processor architecture.

Setting JAVA_HOME

The JAVA_HOME environment variable plays a special role for Java. Even though the JVM executable, java.exe or java, is on the PATH (thus you can execute it by typing the name java without specifying directory on the Command Prompt) (Terminal), it is recommended that you use the correct Java installation to set this environment variable. The value of the variable should point to the installed JDK. There are many Java-related programs, for example, Tomcat or Maven, that use this variable to locate the installed and currently used Java version. In Mac OS X, setting this variable is unavoidable.

In OS X, the program that starts to execute when you type java is a wrapper that first looks at JAVA_HOME to decide which Java version to start. If this variable is not set, then OS X will decide on its own, selecting from the available installed JDK versions. To see the available versions, you can issue the following command:

~$ /usr/libexec/java_home -V
Matching Java Virtual Machines (10):
    9, x86_64:    "Java SE 9-ea"    /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
    1.8.0_92, x86_64:    "Java SE 8"    /Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk/Contents/Home
    1.7.0_60, x86_64:    "Java SE 7"    /Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home

You will then get the list of installed JDKs. Note that the command is lowercase, but the option is capital. If you do not provide any options and argument to the program, it will simply return the JDK it thinks is the newest and most appropriate for the purpose. As I copied the output of the command from my Terminal window, you can see that I have quite a few versions of Java installed on my machine.

The last line of the program response is the home directory of JDK, which is the default. You can use this to set your JAVA_HOME variable using some bash programming:

export JAVA_HOME=$(/usr/libexec/java_home)

You can place this file in your .bashrc file, which is executed each time you start Terminal application and thus JAVA_HOME will always be set. If you want to use a different version, you can use -v, with the lower case option this time, to the same utility, as follows:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

The argument is the version of Java you want to use. Note that this versioning becomes:

export JAVA_HOME=$(/usr/libexec/java_home -v 9)

If you want to use Java JDK Early Access version and not 1.9, there is no explanation for the same—fact of life.

Note that there is another environment variable that is important for Java-CLASSPATH. We will talk about it later.