Create Module in JShell in Java 9

安稳与你 提交于 2019-12-21 10:17:25

问题


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?


回答1:


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:


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


来源:https://stackoverflow.com/questions/46532926/create-module-in-jshell-in-java-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!