Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Module in JShell in Java 9

Just exploring new release of Java, its new module system, and playing with jshell as well. Probably my question doesn't have too much sense, but I am just curious.

So I came up with the question: Is there any way to create a module in jshell? Or module can be only created in module-info.java?

like image 411
Sergei Sirik Avatar asked Oct 02 '17 20:10

Sergei Sirik


People also ask

Is JShell is introduced in java 9?

The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. It was introduced in JDK 9. JShell is a Read-Evaluate-Print Loop tool (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results.

How do you set a JShell?

To start JShell, enter the jshell command on the command line. JDK 9 must be installed on your system. If your path doesn't include java-home/jdk-9/bin , start the tool from within that directory.

What is the purpose the implementation of JShell in java 9?

JShell is a new concept introduced in Java 9 version. It provides Java with REPL (Read-Eval-Print-Loop) ability. By using JShell, we can test the java-based logic and expressions without compiling it. REPL acts as an immediate feedback loop and has a great effect on productivity in that particular language.


1 Answers

Modules cannot be created using JShell currently and it is not a Goal of JShell either.

JShell Functionality

The JShell API will provide all of JShell's evaluation functionality. The code fragments that are input to the API are referred to as "snippets". The jshell tool will also use the JShell completion API to determine when input is incomplete (and the user must be prompted for more), when it would be complete if a semicolon were added (in which case the tool will append the semicolon) and also how to complete input when completion is requested with a tab.

A snippet must correspond to one of the following JLS syntax productions:

  • Expression
  • Statement
  • ClassDeclaration
  • InterfaceDeclaration
  • MethodDeclaration
  • FieldDeclaration
  • ImportDeclaration

A snippet may not declare a package or a module. All JShell code is placed in a single package in an unnamed module. The name of the package is controlled by JShell.

In fact trying to use a ModuleDeclaration within JShell is not a recognized syntax as well both evaluating directly or using the /edit:

enter image description here


Yet, the following options can be made to work out effectively to make use of existing modules within JShell along with the snippet evaluations -

--module-path <path>  
      Specify where to find application modules

--add-modules <module>(,<module>)*
      Specify modules to resolve, or all modules on the
      module path if <module> is ALL-MODULE-PATHs
like image 174
Naman Avatar answered Oct 15 '22 04:10

Naman