Introduction to JShell

JShell is a new feature introduced in Java 9. JShell is a REPL (Read-Evaluate-Print Loop), a command line tool which allows developer to coding in java without building projects in IDEs or compiling and running their short code which is quite lengthy task, so if you are familiar with interpreted language like Python or other JVM languages like Groovy or Scala the concept of JShell is familiar to you.

In this article I want to show you how to use JShell and show you simple web scraping using JSOUP with JShell.

First of all you need to install Java 9 JDK available on OpenJDK for download. I assume that you are familiar with installing JDK and setting it PATH in order to makes it available in Command Line or Shell.
I am using JShell on Ubuntu 16.04.

Hello JShell World :

In order to using JShell you need to type “jshell” in your command line. Then for printing simple “Hello JShell World” traditional phrase you don’t need any class, project, public static void main and other boilerplate codes like that. You need only simple one line of code in JShell:

Control Statements :

Let’s write a simple for expression to create fibonacci series. First of all we need an array of integer to store fibonacci series value.

Now our array is ready. now we should initialize first two elements and then loop through array and use fibonacci formula to calculate next value of fibonacci series.

Here I should mention two things:

  • You can use TAB for getting suggestion that is very useful when you are not in an IDE.
  • Unfortunately You should put semicolon when there is more than one line of code like in loops .

Methods :

Creating methods in JShell is an easy task. lets define a method that produce an array of fibonacci number from given size.

Now you have that method also there is a command that shows you all declared methods using /methods command:

Now we have successfully created our method and we can now invoke it:

So everything is Okey. Notice that I didn’t use any Reference holder to save the result of my method. JShell automatically stored it in “$2” and I used it without any problem. There is a usefull commands that shows you declared variables :

We can Reset JShell by using /reset command:

As you can see our declared methods and variables now gone and our JShell is restarted.

Classes :

Making Class is not a difficult task. Let’s make a Person Class:

now we can create Person object:

Interface, Abstract Class and inheritance and other OO concept is available here but you can’t really code a lot with JShell and It’s a hard task to accomplish with JShell but Let’s write Student Class which extends Person Class:

Our Student Class is completed. We can see our declared Classes using /types command:

We can create Student just by making instance of it:

Imports & Classpath:

In Order to use methods and classes defined in other packages you need to import them. By default there are several packages imported, you can see imported stuff by /imports command:

We need to use JSOUP library for parsing HTML. First of all you need to download JSOUP jar file but how to use it? We should add it to our classpath by /classpath command. I’ve downloaded it in my Desktop in jshell folder:

As you can see I’ve imported jsoup-1.9.2.jar from jshell folder in my Desktop, then I tried to import something from org.jsoup.nodes.

IMDb Top 250:

Now you learned anything that was necessary to work with JShell, so Let’s write a simple script that extract Top 250 Movies from IMDb and Let’s store them in a file :D.

I’m not going to teach you JSOUP and how to work with it. You can find article, video tutorial and books about JSOUP and read JSOUP documentation.

The task is simple, first we need a Document, in order to use Document we should import it.

So everything is ready to parse that document. We should iterate over Element of imdbTop Document and use selectors to extract our desired information.

So everything is done!

We can copy and paste our data in a file, also by coding it.It is your homework! store parsed movie in a list and save that list in a file! Is it hard???

So if it is hard for you let me do that for you!
Define an ArrayList of String for storing result in it:

and then reshape our code to store parsed movie to our list:

now we just have to save our list content to a file using Jave NIO:

Only by one line of code! Awesome, Isn’t it?

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *