Search code examples
javaspringgradlejshell

how to set classpath for spring project in jshell


I'm learning to use spring framework and I can't seem to find a tool similar to Django shell_plus. I'd like to be in a java repl in which I have access to all my classes, dependencies and etc. I've googled and found j-shell and spring-shell but spring-shell doesn't seem to satisfy my needs since I need to be able to instantiate and play around with my classes, so I went with j-shell. my only problem is I don't know how to let j-shell know where my classes are. I've tried

$ jshell --classpath "~/Desktop/path/to/project/"

my project directory being:

Project
 |
 +-- build.gradle
 |
 +-- build
 |     | 
 |     +-- libs and etc...
 +-- src
 |  |  
 |  +-- main
 |        |
 |        +-- java
 |              |
 |              com
 |               |
 |               +-- MyClass.java
 |               |
 |               +-- MyOtherClass.java
 |
 +-- settings.gradle
 +-- gradlew
 +-- default.nix

but when in j-shell I don't have access to spring framework or my classes. I'm using emacs on nixos if that matters and nix for dev environment dependencies. appreciate any kind of help, thanks.

i've also tried setting the classpath to the jars located in ~/home/.gradle/

I expect to be in jshell repl with access to springframework classes and ones i've coded myself.


Solution

  • There are a few ways you could set the class path for a jshell session.

    1. On startup, before you enter the REPL:

      $jshell --class-path <your_classpath>
      
    2. After you're already in a running REPL session:

      jshell>/env -class-path <your_classpath>
      

    Notice the difference between --class-path and -class-path? That's not a typo.

    1. The old-fashioned way. Set a $CLASSPATH environment variable before you start jshell:

      $export CLASSPATH=<your_classpath>
      $jshell
      

    There is actually a fourth way which makes the class path persistent between jshell sessions so you don't have to set it every time. It involves creating a .jsh startup file, and setting and saving it. From within a jshell session, see /help to find out more.