How to import external libraries in jshell java 9?

I tried with 9 Build 162 Linux 64-bit with preparation:

  • Downloaded guava-19.0.jar and commons-lang3-3.4.jar to /opt/libs

The following options are available:

  1. Specify CLASSPATH environment variable:

    $> CLASSPATH="/opt/libs/commons-lang3-3.4.jar:/opt/libs/guava-19.0.jar" bin/jshell

  2. Specify classpath with jshell option:

    $> bin/jshell --class-path /opt/libs/guava-19.0.jar:/opt/libs/commons-lang3-3.4.jar

  3. Configure evaluation context within jshell session with command /env, /reset or /reload(these commands are different, you can check out with its help info), take /env as example:

    jshell> /env -class-path /opt/libs/commons-lang3-3.4.jar:/opt/libs/guava-19.0.jar

And then you're able to either import org.apache.commons.lang3.StringUtils or import com.google.common.base.Optional;.


  • You can load maven artifacts into JShell through this (modified) version of JShell.
  • It also supports /cls command to clear JShell Console. See Maven Example below.

enter image description here

Give a try and share your feedback.


Easier way in maven, see In JShell, how to import classpath from a Maven project: In your project directory, run:

mvn com.github.johnpoth:jshell-maven-plugin:1.0:run

If you have a maven pom.xml, you can use https://github.com/bitterfox/jshell-maven-plugin. This uses all dependencies as the classpath. The plugin is not currently in maven, so you need to clone the repo: git clone https://github.com/bitterfox/jshell-maven-plugin.git. Then,

  1. mvn clean install
  2. add the following to your pom.xml:

    <build>
    <plugins>
        <plugin>
            <groupId>net.java.openjdk.shinyafox</groupId>
            <artifactId>jshell-maven-plugin</artifactId>
            <version>1.0-SNAPSHOT</version>
        </plugin>
    </plugins>
    </build>
    
  3. start with mvn jshell:compile