70

I was trying to understand jshell and fumbled to import external library. As of date I couldn't see any suggestion/solution for this.

Can someone please let me know if already figured this out.

1

7 Answers 7

79

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;.

5
  • 2
    Under Windows 7 it works in the same way - set the CLASSPATH environmental variable, which points to the jar file, then use 'import' to import the class/package.
    – y434y
    Apr 6, 2017 at 17:15
  • @Prags I am trying to use option2 and 3 in windows 10 environment with Jshell it is not working do we need to do something else to make it work in Windows10.
    – Beast
    Mar 22, 2019 at 9:16
  • @Beast can you check this, might it help you java67.com/2012/08/…
    – Prags
    Mar 25, 2019 at 9:36
  • 2
    @Beast JShell on Windows requires either double-backslash paths \\ or forward slash paths / separated by semicolon ; - e.g., /env --class-path c:/path/to/jars/file.jar;c:\\another\\path\\to\\jars\\file.jar Jun 30, 2020 at 14:12
  • For me, on windows, I have to omit /env, just --class-path c:/path... Jul 13, 2022 at 19:31
14
  • 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.

2
  • 4
    "/resolve" is not available in the Java 9 release version of JShell
    – Jason
    Dec 8, 2017 at 12:28
  • 1
    yes /resolve and /cls are not there in Java 9. We have added them in our JShell version. Aug 7, 2018 at 22:52
13

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

8

Start Jshell importing all jars from a directory

Let directory is ~/app/java/jars/

jshell --class-path $(ls -d ~/app/java/jars/* | tr '\n' ':')
2

You can load external library with the command:

/env --class-path /path/to/jar/file
1

If you are using Fish shell, you can set the alias in your fish config file. Here's how you can do it. In your fish config ( ~/.config/fish/config.fish ), add

alias jshell "~/.jenv/versions/12.0.1/bin/jshell --class-path (find ~/.m2/repository/ -name \"*.jar\" | tr '\n' ':')"

This will load all the jars in your class path.

Note: Change the jshell path and jars repository path accordingly.

1
  • it has some scripting error but this will work - jshell --class-path $(find ~/.m2/repository/ -name "*.jar" | tr '\n' ':') make sure you don't have too many jars otherwise you will get bash: /usr/bin/jshell: Argument list too long
    – prayagupa
    Feb 8, 2020 at 5:59
0

If you use script like (find -name "*.jar" | tr '\n' ':') to concat, then you alias will be huge string which I don't like.

If you want to include a directory with all jars, the trick is to use colon (:) first and then the directory name as below -

alias java-shell="~/jdk-10.0.2.jdk/Contents/Home/bin/jshell --class-path :<dir-name>/jars/* "

Otherwise it won't work properly. I wish they should have make it cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.