How to import external libraries in jshell java 9?

Akshay picture Akshay · Mar 30, 2017 · Viewed 15.1k times · Source

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.

Answer

shizhz picture shizhz · Mar 30, 2017

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