Java 9 - Getting Started
Java 9
GETTING STARTED
GETTING STARTED
___
INTRODUCTION
Java 9, also known as Java JDK 9, is the new, upcoming version of the Java SE platform.
New Features
There are several new features in this Java JDK version 9 release, including:
- Java Platform Modules System (JPMS) a.k.a “Project Jigsaw” - introduces a new category to the Java language, called “module”, which serves as a container for grouping related packages.
- JShell - a new Read-Eval-Print Loop (REPL) for writing and executing Java language code as snippets in a command shell.
- Factory Methods for Collections.
- Optional class Enhancements.
- Stream API Enhancements.
- Concurrency updates.
- Process API updates and many more.
Start Working With Java 9
To begin working with Java 9, do the following:
Download the JDK - Java SE 9 Development Kit (JDK 9)
The JDK 9 Early-Access Builds can be obtained online, from http://jdk.java.net/9/. To download the installer for your Operating System, simply go to the webpage at http://jdk.java.net/9/ and click on the “Accept License Agreement” radio button, to accept the Early Adopter Development License Agreement. Once you do that, you may then proceed with downloading the JDK by right-clicking the appropriate link and selecting “Save link as...”, to save the installer to a directory on your local disk drive.
For 64-bit Windows OS, the Java 9 JDK installer’s filename is jdk-9+181_windows-x64_bin.exe and it’s currently about 317MB in size on disk, after download. It is also a good idea to download the API Javadoc which is also found on the same JDK9 download webpage.
Installing the Java 9 JDK
To install the JDK on 64-bit Windows PC, do the following:
- Double-click the downloaded installer file, jdk-9+181_windows-x64_bin.exe, to launch/run it. This will start the installation wizard, beginning with the Welcome screen (see screenshot shown below).
- Click the Next button to go to the Select Optional Features screen.
- Most Java programmers (myself included) normally would have multiple versions of Java JDKs installed on their dev workstations. As a matter of personal preference, I choose to install all my Java SE JDKs inside a directory path named, “C:\javaplatform\se”. I currently have both JDK 7 and JDK 8 installed in directories named, “C:\javaplatform\se\7” and “C:\javaplatform\se\8”, respectively. Therefore, I will naturally proceed with installing this JDK 9 into a directory path named, “C:\javaplatform\se\9”. To do this, click the “Change” button and enter the installation directory path as “C:\javaplatform\se\9\jdk” and click OK.
- Click the Next button to complete the JDK installation and also the installation of the Public JRE that follows suit.
- After both installations have completed, next thing to do is to set the JAVA_HOME environment variable to point to the jdk9 install directory at “C:\javaplatform\se\9\jdk” and also append/add the directory, “C:\javaplatform\se\9\jdk\bin” to the Path environment variable.
However, as a matter of personal preference, again, I choose not to make these couple of changes at this time, simply because I prefer to keep Java JDK 8, as my default JDK version, for now. And will instead be explicitly cd-ing to/selecting Java JDK9 whenever I want to work with it.
- To test/check the Java JDK 9 version info, simply open a Windows command prompt and cd to the jdk9 install directory’s bin folder - “C:\javaplatform\se\9\jdk\bin” and execute the commands -
- C:\javaplatform\se\9\jdk\bin>javac --version
- C:\javaplatform\se\9\jdk\bin>java --version
These display the version info for Java Compiler and the Java Runtime Environment executable (JRE), respectively, as can be seen in the screenshot below:
- Next, to install the Java JDK 9 API Javadoc, simply unzip the downloaded “jdk-9_doc-api-spec.tar.gz” file into a directory named docs,inside the jdk9 installation directory, “C:\javaplatform\se\9”. You can do this using a suitable tool like 7zip. To open and browse the JDK9 API Javadocs, just double-click the index.html file found in the docs folder produced from the unzipping, to load it in your default browser. (see screenshot below)
Coding Your First Java 9 Console Application - HelloWorld Java9
Having successfully installed the Java JDK 9, now we proceed with creating our first Java 9 application. This will be a console application that will simply print-out the iconic message, “Hello World Java9!” to the screen. To implement this, do the following:
Make a new directory for the app
Open the Windows command prompt and run the following command:
C:\>mkdir java9apps\helloworld && cd java9apps\helloworld
This creates the directory for the app and cd’s into it - c:\java9apps\helloworld>
Create a directory for source code, named, src
On the same Windows command prompt, execute the command c:\java9apps\helloworld>mkdir src
Create a directory for compiled code, named, bin
On the same Windows command prompt, execute the command c:\java9apps\helloworld>mkdir bin
Launch open the code editor
For this, I am using Visual Studio Code as my code editor. To launch it open on this “helloworld” directory, simply execute the following command - c:\java9apps\helloworld>code .
This will open VSCode and load the helloworld directory, showing the “src” and the “bin” directories inside it.
Note: My choice of using Visual Studio code for this is just another matter of personal preference. You may use some other Code Editor like Atom, SublimeText, Notepad++ etc. or even plain-vanilla Windows NotePad will do just fine, since we are merely using it to edit source code. All compilation and execution will be done on the command prompt. Also, if you prefer to use a full-blown IDE, note that most Java IDEs do not yet have full 100% support for Java 9 coding, since it is still fairly new and still currently undergoing development. However, from my investigation, the one IDE that seems to have a fairly decent support for Java 9 at the moment is JetBrain’s IntelliJ IDEA version 2017.2.2.
Create a module named, “com.obinna.java9app.helloworld”
We apply the new Java 9 modules concept, by creating a module named, ‘com.obinna.java9app.helloworld’, inside which our helloworld app will reside. To do this within Visual Studio Code, we simply create a directory (folder) named, ‘com.obinna.java9app.helloworld’, inside the “src” directory (folder).
Create the module declaration source file named, “module-info.java”
Create a file named, module-info.java inside the module folder and in it enter the following lines of Java code:
module com.obinna.java9app.helloworld {
}
We are leaving the curly’s here empty as we aren’t requiring packages/classes from any other modules, for this simple HelloWorldJava9 app. However, the “java.base” module is implicitly imported, since every module we create will have a dependency on packages exported from the “java.base” module. This fact can be seen when we run javap on a compiled module-info.class file, as seen in the screenshot below.
This shows that, even if we omit the “requires java.base” statement in our module declaration code, the compiler inserts it during its compilation process.
Create the main source file named, “HelloWorldJava9App.java”
Create this source file to be in a package named, com.obinna.java9app.helloworld. To do this, create the appropriate chain of sub-directories (or sub-folders) structure inside the “com.obinna.java9app.helloworld” module folder and add the source code into the HelloWorldJava9App.java source file, as shown in the screenshot below.
So, our Java 9 modular project structure will then be as shown in the screenshot below -
Compile the source files
Execute the following on the command prompt -
c:\java9apps\helloworld>C:\javaplatform\se\9\jdk\bin\javac -d bin\modules\com.obinna.java9app.helloworld src\com.obinna.java9app.helloworld\module-info.java src\com.obinna.java9app.helloworld\com\obinna\java9app\helloworld\HelloWorldApp.java
This will compile the 2 source files (module-info.java and HelloWorldJava9App.java) and output the compiled .class files in the directory named, bin\modules\com.obinna.java9app.helloworld, as specified by the -d option.
Run the HelloWorldJava9App
Execute the following on the command prompt -
c:\java9apps\helloworld>C:\javaplatform\se\9\jdk\bin\java --module-path bin\modules -m com.obinna.java9app.helloworld/com.obinna.java9app.helloworld.HelloWorldJava9App
The -m option is used to specify the name of the module to run, followed by the classname to be executed.
As expected, this will print the message, Hello World Java9 App!, on the console, as shown in the screenshot below.
Package the App in a distributable JAR
Let’s first create a new directory at “c:\java9apps\helloworld\dist” where we will place the jar to be produced. To do that, we execute the command -
c:\java9apps\helloworld> mkdir dist
Next, we then package the HelloWorldJava9App into a executable jar file named, “helloworldjava9app.jar” and output it to the dist directory. To do this, we execute the command -
c:\java9apps\helloworld>C:\javaplatform\se\9\jdk\bin\jar --create --file dist\helloworldjava9app.jar --main-class=com.obinna.java9app.helloworld.HelloWorldJava9App -C bin\modules\com.obinna.java9app.helloworld .
Re-run the HelloWorldJava9App, this time, using the JAR
Execute the following on the command prompt -
c:\java9apps\helloworld>java -jar dist\helloworldjava9app.jar
Re-run the HelloWorldJava9App, this time, using the JAR and specifying the classpath
Execute the following on the command prompt -
c:\java9apps\helloworld>java -cp dist\helloworldjava9app.jar com.obinna.java9app.helloworld.HelloWorldJava9App
Re-run the HelloWorldJava9App once again, this time, using the JAR and specifying the module-path as the ‘dist’ folder
Execute the following on the command prompt -
c:\java9apps\helloworld> java --module-path dist -m com.obinna.java9app.helloworld
Notice that we do not have to specify the classname following the modulename, as we did before. This is because we used the --main-class option inside our jar file creation command, which gets the information baked into the module; hence specify the classname is no longer required when executing the module.
Conclusion
This concludes the getting started steps for programming with the new Java JDK 9. In these steps, we have implemented a simple HelloWorldJava9 console app as a Java 9 module, using the new Java Platform Modules System.
In subsequent blog-posts, we will continue our exploration of Java 9, by examining the various other new features that will be released with JDK 9.
-----------------------------------------------------------------------------------------------
Comments
Post a Comment