Saturday, July 2, 2016

Changing Your JDK on OS X

So you installed the newest and greatest JDK, maybe an early release. You have buyer's remorse, and want to go back to an older version of the JDK.

Good luck with that.

I'm joking!

First you need to know what versions of the JDK are currently on your OS X box. Check in the following directory.


That should have a list of your installed JDK's.

Alternatively, you can see your installed JDK's using the following command.

/usr/libexec/java_home -V

This list may include additional JDK's if you have a computer that's been around the block a few times.

Now, the real trick is running the one you want. If you just want to run a specific command using a specific JDK, then you might consider using the java_home tool.

Tuesday, June 28, 2016

JShell Commands

JShell has a great collection of command descriptions available from the '/help' command. However, sometimes the terse descriptions given in JShell itself are not quite enough. So, here is a somewhat complete list of JShell commands with descriptions for your viewing pleasure.

The JShell REPL commands work give you a way to manipulate failed, overwritten, dropped and start-up snippets and even other commands. All commands start with a forward slash (/).

For those new to JShell, snippets are little chunks of Java code. For a quick introduction to JShell, see my Java 9 REPL tutorial. Once you get accustomed to JShell, you will realize there is not much of a limit to what you can do with it.

Thursday, June 23, 2016

JShell (Java REPL) Uses

This post explains the uses of the Java JShell REPL. There are three main uses for Java 9's new JShell. First it's great for education. Second, it's great for investigation of APIs that are unfamiliar to you. Third, it is wonderful for quick prototyping of tricky code. So the Java REPL is best suited for education, investigation, and prototyping.

JShell REPL Needed for Schools

Schools have special needs when it comes to teaching basic Java programming. For example, when you're teaching a fist time programmer, especially a child, how to do basic Java, you don't want to start out by explaining packages, classes, methods and scoping. Here is the most basic first Java program you could introduce a child or first-time programmer to.

public class HelloWorldWithoutREPL {
  public static void main(String[] args) {
    System.out.println("Hello World!");

Compare that to the following introductory program you can use when JShell is the platform for showing basic code to a student.

printf("Hello World!")

Wednesday, June 22, 2016

Java 9 REPL (JShell)

JShell is the name of the new Java 9 REPL. Install Java 9, and you'll have access to a shiny new JShell. If you're using an early-access version on Mac OS X like I am, you'll find JShell in the library hidden away in a path similar to the following.


Running JShell on any platform presents you with with a 'jshell>' prompt. If this is your first time using JShell, I recommend you start by typing '/help' to get a list of valid commands. Here are the results of typing '/help' in JShell.

Thursday, June 2, 2016

Hello World Java

Let's start learning Java with the most basic Java program. That of course is the Hello World program for Java.

To make life easier when testing simple Java code snippets, I've embedded a Java REPL in a page. Just follow the link above to the Java REPL. Also since Java 9, you can just use Java's built in REPL by typing 'jshell' from the command line.

Type the following command in the REPL.

System.out.println("Hello World!");

and then press enter. You will see the following.

Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_72 on Linux 3.13.0-49-generic
Welcome to JavaREPL Web Console version 302
java> System.out.println("Hello World!");
Hello World!

That's all there is to it! You've written your first Java code.