Skip to content

Instantly share code, notes, and snippets.

@thomasdarimont
Last active October 17, 2020 13:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/59006cb3560eebc8e1bb5dfff61d62db to your computer and use it in GitHub Desktop.
Save thomasdarimont/59006cb3560eebc8e1bb5dfff61d62db to your computer and use it in GitHub Desktop.
Run a jshell instance with a classpath derived from maven pom.xml file

Linux / OSX

Note that this example uses sdkman.

CP=$(mktemp) && \
mvn dependency:build-classpath -Dmdep.includeScope=compile -Dmdep.outputFile=$CP -q -f ./pom.xml && \
CP=$(cat $CP) && \
$(sdk home java 11.0.8.hs-adpt)/bin/jshell --class-path $CP

Example (Linux)

CP=$(mktemp) && \
mvn dependency:build-classpath -Dmdep.includeScope=compile -Dmdep.outputFile=$CP -q -f ./pom.xml && \
CP=$(cat $CP) && \
$(sdk home java 11.0.8.hs-adpt)/bin/jshell --class-path $CP
|  Welcome to JShell -- Version 11.0.8
|  For an introduction type: /help intro
jshell> 

Windows (Powershell)

$CP = (New-TemporaryFile).FullName; `
mvn dependency:build-classpath "-Dmdep.includeScope=compile" "-Dmdep.outputFile=$CP" -q -f ./pom.xml; `
$CP = Get-Content $CP -Raw; `
& "C:/Program` Files/OpenJDK/jdk-11/bin/jshell" --class-path $CP

Example (Windows Powershell)

> $CP = (New-TemporaryFile).FullName; `
>> mvn dependency:build-classpath "-Dmdep.includeScope=compile" "-Dmdep.outputFile=$CP" -q -f ./pom.xml; `
>> $CP = Get-Content $CP -Raw; `
>> & "C:/Program` Files/OpenJDK/jdk-11/bin/jshell" --class-path $CP
|  Welcome to JShell -- Version 11.0.2
|  For an introduction type: /help intro

jshell>
@thomasdarimont
Copy link
Author

thomasdarimont commented Oct 16, 2020

@mehiel pointed me to the jshell-maven plugin, which makes the above easier :)

mvn com.github.johnpoth:jshell-maven-plugin:1.2:run

Note that this requires JDK 11+!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment