graalvm

Chase 2023-03-03T18:43:08.457359Z

I want to explore using GraalVM to build out a little CLI app and am curious about having several JVM's on my system. I'm using Temurin-17.0 right now and would like to keep that as my default. Can I install Graal, use it to bundle up my app, then get back to Temurin pretty easily or is this a big hassle? I'm using Debian

borkdude 2023-03-03T18:51:17.011179Z

Yes. Just set GRAALVM_HOME and JAVA_HOME for your project

➕ 1
borkdude 2023-03-03T18:51:48.045389Z

I usually just download GraalVM in my Downloads folder and write a script that takes into account the above GRAALVM_HOME env var

borkdude 2023-03-03T18:52:11.541429Z

If the CLI app is little, perhaps #babashka + #babashka-bbin is also an idea.

borkdude 2023-03-03T18:52:49.047039Z

Here is an example how I write a graal CLI build: https://github.com/borkdude/clj-native-sound-demo/blob/857fa5124267e93d36fff86db85b645ce784376a/bb.edn#L5-L10

Chase 2023-03-03T18:53:52.285539Z

It is small. Does babashka work with clj-http? For some reason I was having issues with http-kit

phronmophobic 2023-03-03T18:54:10.737129Z

I have the following in my .bash_profile

function graalvm() {
    export GRAALVM_HOME=/Users/adrian/workspace/graalvm/graalvm-ce-java19-22.3.1/Contents/Home
    export JAVA_HOME="$GRAALVM_HOME"
    export PATH=$JAVA_HOME/bin:$PATH
    java -version
}
You can switch to using the graalvm jvm just by typing:
$ graalvm

Chase 2023-03-03T18:54:55.472659Z

nice. And then how do you get back?

phronmophobic 2023-03-03T18:55:32.058039Z

You could set it up to restore, but I just close that terminal and start a new one 😄

👍 1
borkdude 2023-03-03T18:56:07.985619Z

Also, I almost never find the need to "get back" since GraalVM works perfectly fine as a normal JVM as well

borkdude 2023-03-03T18:56:58.866039Z

> It is small. Does babashka work with clj-http? For some reason I was having issues with http-kit No, but it has https://github.com/babashka/http-client which is built on the java.net.http package

phronmophobic 2023-03-03T18:57:50.359999Z

Compiling native-image can be fairly slow though. Because of that, bb and friends is a great place to start since it's precompiled.

Chase 2023-03-03T18:58:25.161459Z

Yeah I'm going to give bb a shot since I've wanted to get some experience with it anyways. That built in http-client looks perfect for my simple needs

Chase 2023-03-03T19:05:17.386289Z

And clojure.data.json works with bb too? Sorry this got so derailed

borkdude 2023-03-03T19:06:12.535299Z

in bb, cheshire.core is the built-in json library. clojure.data.json works from source, but it's slow if you have big payloads since it's interpreted.

Chase 2023-03-03T19:07:11.473079Z

I can try with cheshire , it's a small 40 line program

borkdude 2023-03-03T19:08:56.766569Z

you can do it like this, if you want to use clojure.data.json:

(defmacro if-bb [then else]
  (if (System/getProperty "babashka.version") then else))

(defn parse-json [s]
  (if-bb
    ((requiring-resolve 'cheshire.core/parse-json) ...) s)
    ((requiring-resolve 'clojure.data.json/read-str) s)))

borkdude 2023-03-03T19:09:42.951449Z

reader conditionals are also supported:

#?(:bb ... :clj ...)

lread 2023-03-03T21:38:35.860459Z

I personally use https://sdkman.io/ to switch between JDKs. Works nicely for me.

👍 1