简体   繁体   中英

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.

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.

I made a Gradle plugin to run a jshell console with all the libraries of your project in the classpath: jshell-plugin , once configured in your build setup, you only need to run:

$ gradle --console plain jshell

Also the README instructions have a special section to setup it in a Spring Boot project and be able to access to the business objects (the beans in the Spring context: #spring-boot-applications :

jshell> var myUserService = ctx.App.getBean(MyUserService.class)

jshell> ctx.App.ppjson(myUserService.getByUsername("admin"))
{
  "name" : "Jhon",
  "lastName" : "Due",
  "username" : "admin",
  "age" : null
}

If you are not using Gradle and you have an old Spring Boot 1.x project, you can use another project I made that does not work with modern Spring Boot applications, but at least you don't need Gradle or the jshell from a JDK9+ distrubution to play with your code and the spring's beans: spring-ctx-groovy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM