using Java 9’s jshell for experimenting

In Java 8, Oracle introduced the new Nashorn engine and is encouraging the use of jjs. It’s a RPEL (read print evaluate loop) for JavaScript. Since JavaScript can call Java, this allows experimenting with Java APIs like you do in other languages like Python and Ruby. I actively disliked it and even wrote a blog post about what turned me off. Java 9 introduced JShell for this same purpose except it works with Java rather than a level of indirection through JavaScript.

In this blog post, I repeat the same experiment with JShell (using the Java 9 early access download) to see if it is any better. Again I used the exercises from Cay Horstmann’s book, Java SE 8 for the Really Impatient so it would be an equivalent experiment.

Exercise 1 – Play … to experiment with an API you want to explore; did you find it easier than writing test programs in Java?

With Nashorn/jjs in Java 8, I had three issues. Let’s see how jjs compares:

Nashorn/jjs JShell
Strike 1 – needing to type in the package name of classes. Better. At least common packages are included now like java.util. For others, like LocalDate, you still need to type the package name. Which often means looking up the package name. I rely heavily on “organize imports” in my IDE for package names.
Strike 2 – no tab complete Success! JShell has tab complete for packages, classes and methods
Strike 3 – no up arrow support Success! JShell lets you you use the up/down arrows to go through your command history.

Having solved these three issues, the question is a better one. If I’m on a UNIX system or don’t have my IDE open, JShell would definitely be faster. If I already have the IDE open, I have a “PlayTest” program with a main method already available to me so it is a tie. But that’s just my workflow. JShell looks good.

Note: I learned on Twitter that:

wickund: @jeanneboyarsky jjs in JDK9 uses the same API as jshell (JDK9), so there is code completion and up arrow.

Which means my issues with Nashorn are gone in Java 9. Thanks Jim Laskey

Exercise 2 – Using …. the stream library iteratively work out a solution to the problem to print a filtered list of unique long words in sorted order and see how it compares to your usual workflow.

Here was my workflow with JShell:

  1. Create the test file in a text editor (vi)
  2. Run ./jshell
  3. Create a path object
    Attempt # Code Error Comments
    1 jshell> Path paths = Path.get(“path/words.txt”) Success! This package is included in the list of known ones so I didn’t even have to look anything up.
  4. Read the file
    Attempt # Code Error Comments
    1 jshell> List<String> list = Files.readAllLines(path) Success
  5. Filter the long words
    Worked.

    Attempt # Code Error Comments
    1 jshell> list.stream().grep({ l -> l.length() > 12}) cannot find symbol This is completely wrong. Aside from the stream() call, it is using Groovy syntax. (I code Groovy at a command line so my fingers started there. I did the same thing when I was doing this with Nashorn)
    2 jshell> list.stream().filter( l -> l.length() > 12) Success
  6. Add sorting and uniqueness
    Attempt # Code Error Comments
    1 jshell> list.stream().filter(l -> l.length() > 12).sorted().forEach(System.out::println) Sorting worked
    2 jshell> list.stream().filter(l -> l.length() > 12).sorted().distinct().forEach(System.out::println) Uniqueness filter worked

 My thoughts

This was 6 attempts/iterations using JShell.  This is compared to 17 in Nashorn! JShell is clearly a superior tool for prototyping Java. Part of this is because they implement common RPEL features. And part is because they don’t try to shoehorn a rhinoceros into being a Java RPEL. Seriously, many of the problems with Nashorn came from mixing Java and JavaScript while trying to prototype Java.

OCA/OCP 8 Programmer Practice Tests AVAILABLE NOW!

Jeanne and I are really excited to announce that our new book OCA / OCP Practice Tests: Exam 1Z0-808 and Exam 1Z0-809 is now shipping. In fact, we just received our copies today! The book is available in both print and digital formats from Amazon and other major book retailers. In addition, purchasing this book grants the holder access to Sybex’s interactive test bank!

This book serves as worthwhile companion to our previous two books, now available as set, The OCA / OCP Certification Kit. With this book you are really getting two books in one, as Jeanne and I packed over 1000+ questions into this text, covering both the OCA and OCP exams. To better prepare you for test day, we also threw in two simulated OCA/OCP exams. Whether your taking the OCA exam now and thinking about the OCP exam down the road, or taking the OCP exam and need a refresher on the cumulative material, this book has got you covered!

This book covers 100% of all exam objectives in these practice tests, which means you’ll be ready for:

  • Java basics, class design, and data types
  • Using decision and loop constructs
  • Building and using arrays
  • Methods, encapsulation, and inheritance
  • Mastering Java Streams
  • Working with the Date/Time API and Localization
  • Understanding design patterns and principles
  • Writing Lambda expressions and functional interfaces
  • Exceptions and assertions
  • Java File I/O (NIO.2)
  • Concurrency and localization
  • JDBC and database applications

Jeanne and I put a lot of time, energy, and planning into this new book and we really hope our reader enjoy it!

my second 3D print

After completing my test run, I didn’t have a lot of time so I wanted to start with something simple. Preferably one I could use as gift on an upcoming vacation.

I had taken a class a year or two on the basics of 3D printing which used TinkerCAD. I like that it is free and easy to use. I’ve forgotten most of what I’ve learned so will get to relearn when I have more time. I did remember enough to assemble two simple objects together. It seemed good enough for a first print. Here’s the design:

TinkerCAD lets you download a .stl file so I was all set to print. Actually printing it, I learned a few things about the software:

  • There’s a size option when printing. I thought it was inches. It’s not. It’s scale. I printed a bit bigger than I wanted, but that’s ok.
  • The abort button doesn’t work. Nor does the emergency stop. What does work is killing the power or unplugging the printer.
  • Closing the software doesn’t work on Mac. Force quit does. I’m not sure if this is a problem with the software or with Mono.
  • The status screen doesn’t let you command tab to it. Instead you have to do the show all windows thing to get to it.

It took a few hours to print (this printer is small so therefore less speedy than some others). It was tricky to get off the bottom of the printer bed. Online suggested a scraping tool. I couldn’t get it with a putty knife. What worked was a nail file.

And the result: