How to implement JShell using JavaFX in Java 9?

JShell is an interactive tool used to implement sample expressions. We can implement JShell programmatically using JavaFX application then we need to import a few packages in the java program listed below

import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;
import jdk.jshell.VarSnippet;

In the below example, implemented a sample Java FX application. We will enter different values in the text field and press the "eval" button. It will display values with corresponding data types in a list.


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import java.util.List;
import jdk.jshell.JShell;
import jdk.jshell.SnippetEvent;
import jdk.jshell.VarSnippet;

public class JShellFXTest extends Application {
   public void start(Stage primaryStage) throws Exception {
      JShell shell = JShell.builder().build();
      TextField textField = new TextField();
      Button evalButton = new Button("eval");
      ListView<String> listView = new ListView<>();

      evalButton.setOnAction(e -> {
         List<SnippetEvent> events = shell.eval(textField.getText()); -> convert(event)).filter(s -> s != null).forEach(s -> listView.getItems().add(s));
      BorderPane pane = new BorderPane();
      pane.setTop(new HBox(textField, evalButton));
      Scene scene = new Scene(pane);
   public static String convert(SnippetEvent e) {
      if(e.snippet() instanceof VarSnippet) {
         return ((VarSnippet) e.snippet()).typeName() + " " + ((VarSnippet) e.snippet()).name() + " " + e.value();
      return null;
   public static void main(String[] args) {


Updated on: 02-May-2020


Kickstart Your Career

Get certified by completing the course

Get Started