5 External Code

External classes are accessed from a JShell session through the class path. External modules are accessed through the module path, additional modules setting, and module exports setting.

Setting the Class Path

You can use external code that is accessible through the class path in your JShell session.

Set the class path on the command line as shown in the following example:

 % jshell --class-path myOwnClassPath

Point your class path to directories or JAR files that have the packages that you want to access. The code must be compiled into class files. Code in the default package, which is also known as the unnamed package, can’t be accessed from JShell. After you set the class path, these packages can be imported into your session:

jshell> import my.cool.code.*

You can also use the /env command to set the class path, as shown in the following example:

jshell> /env --class-path myOwnClassPath
|  Setting new options and restoring state.

The /env command resets the execution state, reloading any current snippets with the new class path setting or other environment setting entered with the command.

Setting Module Options

Modules are supported in JShell. The module path can be set, additional modules to resolve specified, and module exports given.

Module options can be provided in options to the /env command or on the command line as shown in the following example:

 % jshell --module-path myOwnModulePath  --add-modules my.module

To see current environment settings, use /env without options. The following example includes class path information that was set in Setting the Class Path:

 jshell> /env
 |     --add-modules my.module
 |     --module-path myOwnModulePath
 |     --class-path myOwnClassPath

For details about the options, enter the following command:

jshell> /help context