JDK 9 and JShell

Intro

I recently got to know about this official Java REPL (Read-Eval-Print-Loop) or JShell project. It is named as Kulla and you can visit here to see the project’s home. This is pretty much same like the Python’s IDLE (If you have used it before) and a great way to exercise your code in real time. Also the good thing is that this project will be available as a part of JDK9 among with some other cool features.

Anyhow I managed to get it run on own and have tried few exercises too. Here, take a look

How do I get it to run

I haven’t tried this on Windows, only on POSIX based systems (Linux).  But I believe the precompiled jar will work on Windows. You can give it a go and see.

Easy way

If you want to try out REPL right away there’s this precompiled Jar that you can use . What you’ll need is

Once these are in place you just need to set the JAVA_HOME to your / path / to / JDK 9. Then execute the following -jar command:-

$ java -jar kulla.jar

You will be entered in to the JShell.

Hard way

NOTE: The whole build process can take up to 20-30 minutes or more, so brace yourself.

  • Make sure you have set the JAVA_HOME
  • You also need Mercurial. If you are on Ubuntu just give sudo apt-get install mercurial
  • The the follow these commands to get kulla-dev branch built
    • hg clone http://hg.openjdk.java.net/kulla/dev kulla-dev
    • cd kulla-dev
    • sh get_sources.sh
    • bash configure –with-boot-jdk=/path/to/jdk1.9
    • make clean images
    • make install  (optional)

OK, kulla-dev branch is now built, hopefully without any errors. Now lets see how we can build and run the REPL. I’m extracting these information from official README under Kulla dev branch.

Download JLINE2 from Maven, and set the environment variable JLINE2LIB to point to the downloaded jar file.

Building REPL:-

  • cd langtools/repl
  • bash ./scripts/compile.sh

Running:-

  • bash ./scripts/run.sh

If everything goes fine you’ll be entered to the JShell without any issues.

Features

I will add a summary of features that you’ll find useful when using the REPL.

  1.  REPL has networking support. Yes you can work with java.net
  2.  Semicolone is optional giving you a flexibility like most of REPL’s out there
  3.  It has some useful help commands that you can use to improve your productivity. /help list those commands
  4. Checked exceptions are not valid here. Like in normal Java environment you will not be forced to handle the checked exceptions. REPL will be handling it in the background
  5. Expressions will also work out of the box here. Arithmetic, String manipulations, method calls .etc

Here I found a good tutorial that might be useful. It has some basic to intermediate exercises that you can follow go get familiar with the JShell/REPL

Advertisements

JAXB + Maven – Xml to Java

Situation:

Assume there’s a situation where you want to generate Java sources from a xml schema definition (xsd).  And generate it constantly whenever you want it if the xsd’s got updated.

Solution:

There are quiet a few out tools/libs out there but I found this particulate set (Maven and JAXB2) works well for me, just because it’s pretty straight forward. But you may prefer a different approach.

In to the action:

Let’s create a very basic maven project (assume you have already set up Maven and Java).

mvn archetype:generate -DgroupId={com.jaxb.hello} -DartifactId={HelloJAXB2} -DarchetypeArtifactId=maven-archetype-quickstart  -DinteractiveMode=false

Once the project is created open the pom.xml file in edit mode and add following two plugins

  1. JAXB-2 Maven Plugin
  2. Maven Source Plugin

[Since this is a sample project you may have to define the maven <plugins /> sections]

plugins-section

Carefully notice the sections

                            <generatePackage>com.jaxb.hello</generatePackage>
<schemaIncludes>
<include>helloschema/*.xsd</include>
</schemaIncludes>

<generatePackage> defines the place where you want to put the generated sources for the xsd’s

<schemaIncludes> defines the place where you place your .xsd files.

Now keep all that in mine lets see the project structure

project-structure

As you can see I just added a single .xsd to the location helloschema/

Once all these are in place you just have run the Maven target.

mvn package

maven-build

[Here I have skipped the tests for the convenience]

If everything goes smoothly you’ll see two .jar files in the target folder

final-build

Sources are bundled in the HelloJAXB2-1.1-SNAPSHOT-sources.jar

and you can find it also in generated-sources folder as well

generated-sources

Managing two Java versions (Windows)

Ever had a problem of working with two Java versions ? Well this post will explain the basic steps of switching between two or more Java versions that is currently installed on your Windows machine.

When installing Java on your Windows machine it will

  • Add/overwrite specific registry keys
  • Add/overwrite relevant exe’s to system (system32)
  • Environment variables (sometimes this we have to manually if we are installing the second Java version on same machine that has another version of Java already installed)

These approaches will change/modify both Java run-time and JDK on a machine. So when we are switching between different Java versions we have to change these locations by manually or using an automated way. In this post I will only explain the manual approach.

Now I assume you may have already installed a one Java version on your machine so go to the below link and download the latest Java version (current latest version is Java 7)

When you visit the above link you will see they have listed different versions of Java on the table titled as Java Platform, Standard Edition. Download the JDK version (it will also provides the JRE) relevant to your system arch type either 64bit version on 32 bit version. After downloading JDK (ie:  jdk-7-windows-i586.exe) you can now install the Java on your machine. Likewise you can download and install many Java versions on a single machine. Now lets see how you can switch or change a Java version.

First go to the installation location of you all Java versions.

ie:

F:\Softwares\Java\jdk1.6.0  – for Java 6                                                                                                                                                  F:\Softwares\Java\jdk1.7.0 – for Java 7

Before you do any changes lets check the current Java version. Open your command line (or cmd) and type java -version.It will give you a output similar to this.

java version “1.6.0_20”
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

of so it’s Java 6 now we want to switch to Java 7

go to bin folder of your Java installation which you are going to switch (Java 7)

F:\Softwares\Java\jdk1.7.0\jdk1.7.0\bin ——– now location three exe files mentioned below.

  1. java.exe
  2. javaw.exe
  3. javaws.exe

copy these three exe files to your Windows system32 folder.

C:\Windows\system32

if it asks to overwrite go ahead and do it. Ok now the Java 7 run-time is successfully inserted in to our system but before we proceed we need to another small change to our Windows registry. That change will tell system to use Java 7 as the current system run-time (which is located in system32).

To change the relevant registry key you need an Administrative privileges so make sure you log in as an Administrator of a system. Now go to the registry editor simply type regedit on you run program application. Now location the key

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\CurrentVersion

You may see current version as 1.6 modify it to 1.7. That’s it now you are almost done you can now check the Java version again now it says.

java version “1.7.0”
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) Client VM (build 16.3-b01, mixed mode, sharing)

Yes, it has changed to Java 7 the hard part is over now. You can now change the Windows environment variables to use JDK on your machine just like you did before with your previous Java version. Now if you want to switch back again to Java 6 you can follow the same steps mentioned above.

As you can see there’s a bit of hassle when you have to change the Windows registry and copying exe files all by yourself. So you can always go ahead and write a Windows batch process or a simple C/C++ script to automate these steps.