A simple script to launch JShell against Spring Boot packaged classpath.
The script can be useful to quickly prototype on non-development environments (like Docker containers) where environment difference matters (e.g. OS features, file system structure, network policy restrictions, etc).
-
Make sure you have Java 11+ installed on target environment
-
Copy
jshellw
wrapper script into destination, e.g.
$ sudo docker cp jshellw mycontainer:/microservice/jshellw
- Make the script executable
$ sudo docker exec -w /microservice mycontainer chmod +x jshellw
- Run the script pointing to Spring Boot JAR or WAR archive
$ sudo docker exec -it -w /microservice mycontainer ./jshellw app.jar
The output should look like:
Created temp directory '/tmp/...'. Extracting classpath content...
Extracted 191 files from the archive to '/tmp/.../BOOT-INF'.
Starting JShell with '/usr/lib/jvm/java-11-openjdk-amd64/bin/jshell --feedback verbose --class-path /tmp/.../BOOT-INF/classes:/tmp/.../BOOT-INF/lib/*'...
| Welcome to JShell -- Version 11.0.1
| For an introduction type: /help intro
jshell>
To run the script in Windows just execute the following instead of steps 2 and 3:
java --source 11 jshellw app.jar
To check if classpath has been composed and applied correctly, type /env
and you should see something like:
jshell> /env
| --class-path /tmp/.../BOOT-INF/classes:/tmp/.../BOOT-INF/lib/HdrHistogram-2.1.9.jar:...<other-jars>...
Now you can import any classes from your classpath and work with them in JShell just like you do in your dev environment.
For example:
jshell> import org.springframework.util.StringUtils
jshell> var cleanedPath = StringUtils.cleanPath(".\\..\\core/inst/meg.dump")
cleanedPath ==> "../core/inst/meg.dump"
| created variable cleanedPath : String
You can even launch the application at whole by invoking it's main class, e.g.
jshell> import com.example.spring.boot.application.MainClass
jshell> MainClass.main(new String[0])
but before it make sure that current value of user.dir
system property points to application's home directory (if it matters).
See this Comprehensive Guide or google for java repl
.