Compiling a smart contract in Java

Java is quite different from C/C++/Go/Rust since it’s required JRE to execute. The javac standard compiler produces JVM bytecode from Java source (class file) not a static executable binary.

In this sample, we’ll use GraalVM Native Image to produce static binary that suitable for deploying and executing under QVM environment.

# compile JVM bytecode (Java class file)
javac contract.java

# compile class file to static binary
native-image --static contract

# check the output binary size
wc -c contract
13644368 contract

Compiling Java contract to static binary with the QVM compiler

We already supply a simple compiler container which will perform the most optimized build of turning above sample contract to a static binary which can be run by QVM.

Since you saved the previous sample contract as contract.java in your current directory, you can run the compiler like this:

docker run --rm -v $(pwd):/ws qanplatform/qvm-compiler-java

Afterwards you will end up with a statically linked linux ELF binary called "contract" in your current directory.

# checking output size of QVM compiler
ls -alh contract
-rwxr-xr-x  1 user  group    863K Jun 22 15:48 contract

Last updated