Sergei Sirik Sergei Sirik - 1 year ago 125
Java Question

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
?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download