Graal contains an AOT compiler which makes it really straight forward to compile jars to native binaries. I've currently only tried the community edition on linux. So your mileage may vary.
- http://www.graalvm.org/downloads/
- https://github.com/coursier/coursier
- A working gcc compiler and headers. e.g.:
apt install build-essential zlib1g-dev
Graal ships with substrate vm
which can be used to compile any java byte code to a native image.
We need to pass the correct classpath and main to the native-image
tool.
The simplest way for is to fetch scalafmt and all it's dependecies via coursier.
We are immediately piping it to paste
to convert the newline to :
so that we can directly use it for the classpath argument.
CLASSPATH=$(coursier fetch com.geirsson:scalafmt-cli_2.12:1.4.0 -r bintray:scalameta/maven | paste -s -d: -)
To actually create the binary execute
/path/to/graalvm/bin/native-image -cp $CLASSPATH org.scalafmt.cli.Cli -H:+ReportUnsupportedElementsAtRuntime
we unfortunately need to include the -H:+ReportUnsupportedElementsAtRuntime
because scalafmt uses code which isn't yet supported by the AOT compiler.
- Relative paths don't work. They are always resolved to the session folder in which the native image was generated.