SQLcl with JavaScript on macOS using GraalVM
I needed to use JavaScript capabilities in SQLcl and realized that this does not work anymore with my OpenJDK setup. Jeff Smith blogged about the background details and how he got it working. As it changed a bit, was for Windows, and I am on macOS, I decided to blog my solution. Also, note that I don’t want to mess with my main OpenJDK setup, so I used a way to type a command and switch to GraalVM.
Download GraalVM and extensions
#Download GraalVM from this page. Make sure to select your OS and architecture (ARM64 M macs).
Visit this page and check that you have the correct OS and architecture selected (macOS/aarch64 for M Macs). Then download these three extensions:
- JavaScript Runtime
- Tregex Plugin
- ICU4J Plugin
We also need the ICU4J and Tregex plugins as they are dependencies for the JavaScript plugin.
Be able to switch GraalVM
#- Unzip the GraalVM archive and move it to a location of your choice. I moved it to
/Users/phartenfeller/graalvm/graalvm-jdk-17.0.13+10.1. - Edit your
.zshrcor.bashrcfile and add the following lines:
graal-vm() {
export JAVA_HOME="/Library/Java/JavaVirtualMachines/graalvm-ce-java17-22.1.0/Contents/Home"
export PATH="/Users/phartenfeller/graalvm/graalvm-jdk-17.0.13+10.1/Contents/Home/bin/":$PATH
}
- Run
source ~/.zshrcorsource ~/.bashrcto apply the changes. - Run
graal-vmto set the environment variables. - Run
java --versionand verify that the output containsOracle GraalVM.
Test if SQLcl works
#sql -nolog
# SQLcl: Release 24.3 Production ...
sql> script
2 ctx.write("Works\n")
3* /
# Should error
# JavaScript engine not found.
# GraalVM with Nodejs/Javascript support is required
Install JavaScript capabilities
#cd /Users/phartenfeller/graalvm/graalvm-jdk-17.0.13+10.1/Contents/Home/bin
# icu4j
./gu -L install /Users/phartenfeller/Downloads/icu4j-installable-jdk-17-darwin-aarch64-23.0.6.jar
# regex
./gu -L install /Users/phartenfeller/Downloads/regex-installable-jdk-17-darwin-aarch64-23.0.6.jar
# js
./gu -L install /Users/phartenfeller/Downloads/js-installable-jdk-17-darwin-aarch64-23.0.6.jar
Test if JS works
#sql -nolog
sql> script
2 ctx.write("Works\n")
3* /
# Works
Important update
#Newer SQLcl versions don’t support Java 17 anymore. I tried upgrading to a newer GraalVM version, but they don’t provide the JavaScript extension anymore.
Instead, I downloaded a separate SQLcl version (24.4.4) and just use this one for scripts that need JavaScript capabilities.
I call it like this:
function sqljs() {
export JAVA_HOME="/Library/Java/JavaVirtualMachines/graalvm-ce-java17-22.1.0/Contents/Home"
export PATH="/Users/phartenfeller/graalvm/graalvm-jdk-17.0.13+10.1/Contents/Home/bin/":$PATH
java --version
/Users/phartenfeller/sqlcl/bin/sql $*
}
## using it
sqljs -name local-23ai-sys
Swap out /Users/phartenfeller/sqlcl for your SQLcl 24.4.4 location.
Conclusion
#Now every time I want to use SQLcl with JavaScript capabilities, I can just run graal-vm and my main Java version switches to the GraalVM version that can run JavaScript. This way I can keep my main Java version for other projects and only switch when needed.
By the way. If you want to easily run a local containerized database with APEX set up in minutes, check out my project uc-local-apex-dev. I have also been busy in December with an Advent Calendar full of 24 short APEX tip videos.