Finally: Java 9

Land Ho!

Tools

Java 9 offers two new tools for working with modules: The first takes care of tasks relating to module dependencies. Among other things, it prepares them as texts or graphs or generates module descriptions for existing JAR files. The run-time environment can be trimmed with JLinker to match a module description. A minimal installation thus shrinks from 275MB (Java 8) to 40MB, reducing the startup time of a simple application on a PC by two-thirds, or about 0.9 seconds.

Thanks to the new -html5 option, the Javadoc tool now generates HTML with a more modern, barrier-free design and appearance. Not only a matter of a modified stylesheet, the generated Java documentation impresses with, among other things, a built-in search for class and method names and tooltips (Figure 2). The displayed classes can be restricted to selected Java modules, or you can view only the modules for a particular class.

Figure 2: Javadoc appears in a new guise in Java 9 thanks to the -html5 option.

Shell Factory

JShell, a more recent addition, allows the use of uncompiled Java as an interpreted language. The idea is not new: Patrick Niemeyer presented a first implementation 17 years ago in the form of BeanShell [4]. However, it never supported the full range of Java and work ground to a stop over the past few years. This situation was reason enough for the Java compiler group to begin work on a new shell code-named Kulla (the Sumero-Babylonian brick god). As a REPL (read, evaluate, print, loop) tool, it allows Java code to be executed line by line and is launched by typing jshell at the command line (Figure 3).

Figure 3: JShell evaluates entries after each newline, much like Python or TCL.

JShell evaluates the input after each newline, as in Python or TCL, whether it is normal Java code or one of the JShell commands. JShell commands all start with a slash; /help shows an overview of the available commands. When a line is executed, Java 9 either automatically creates variables, or the developer has to define them with a type and name. The /var command shows a list of defined variables and their values.

JShell accepts normal and static imports anywhere; /import lists those defined so far. Developers can use JShell not only to test small snippets of code like the DateTimeFormatter here, but it also supports the full syntax of Java 9, from a simple one-liner, through lambda expressions, to defining whole methods and classes.

Fortunately, programmers do not have to type flawlessly at the command line. If you mistype the code, a Java compiler error message appears, and you can retry. As in Bash, you can retrieve and edit previous input using the arrow keys. Expansion of class paths by pressing the Tab key works much the same way.

If you prefer working in a graphical editor, you can launch one by typing the /edit command (Figure 4) or load externally written code from a file with /o. Unlike compiled code, the semicolon at the end of the line is optional, and you don't have to worry about handling exceptions with try and catch blocks. More in-depth integration with the usual suspects (e.g., NetBeans, Eclipse, and IntelliJ) is expected in the next few months; IDEs for Python show what is possible in this direction.

Figure 4: From the new JShell, you can even launch a simple editor.

Running

Work on the run-time library was also intended for Java 9, but there is not much of this left because of the delay. The most interesting new entry is the enhanced process API, which makes it possible, after years of waiting, not only to launch external processes, but also to stop them, access process IDs, and conveniently wait for them to end.

Swing was given some fine tuning. Instead of the obsolete GTK+ 2 library, the current GTK+ 3 library is now used, thus supporting settings for high-resolution displays.

The HTTP client has also seen the writing on the wall with HTTP/2 support. The frame-based protocol offers less overhead and lower latency and is now fully supported, thanks to the new HttpRequest and HttpResponse classes. The HttpRequest class is used to build a request, and the Fluent API lets programmers conveniently specify all the required information. Listing 4 shows an example that presents the results with HttpResponse.

Listing 4

HTTP/2 client

01 HttpResponse response = HttpRequest
02                 .create(new URI("http://www.linux-magazine.com"))
03                 .headers("User-Agent", "Java9")
04                 .GET()
05                 .response();
06
07 LOG.info("status : " + response.statusCode());
08
09 Map<String, List<String>> headers = response.headers().map();
10 for (String key : headers.keySet()) {
11     LOG.info(key + " : " + headers.get(key));
12 }
13
14 String body = response.body(asString());
15 LOG.info("body : " + body);

The question of which elements the Java developers would discard is almost more important than what has been added. To ensure backward compatibility, they have so far not removed a single class, method, or variable. Meanwhile, at least 60 classes and 400 methods are tagged as @deprecated; some of them have already been marked for removal in Java 1.1. After 20 years, Oracle finally has relented and announced that it will remove obsolete parts.

The most prominent victim is the Java applet, which breathes its last breath in Java 10. It provided good service, boosting the popularity of Java in the early years, but it is undesirable in the browser for security reasons, just like Flash or Silverlight. Ironically, the outgoing applet leaves the biggest gap in the area of creating certificates, encrypted data transfer, or signatures; HTML5 lacks equivalent capabilities.

The cleanup will take place in small steps; the first one has already been taken. The @deprecated annotation now comes with two optional attributes forRemoval and since, which mark the classes that will disappear in the future.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy ADMIN Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus
Subscribe to our ADMIN Newsletters
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs



Support Our Work

ADMIN content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.

Learn More”>
	</a>

<hr>		    
			</div>
		    		</div>

		<div class=