Java 21 introduced the Foreign Function and Memory API which greatly simplifies interacting with native code and libraries. With the ability to call system APIs directly from Java, it is now much easier to generate and execute machine code on the fly without relying on C/C++ as an intermediary.
Please note that this prototype only demonstrates what's possible: it is purely exploratory and not intended to advocate for its practical use.
This is a simple hello-world prototype that prints "Hello Machine!" from a function defined in native x86-64 code. It works like this:
- Map a
W|Xregion of memory and copy the machine code there.* - Use the mapped address to invoke the native code with
invokeExact.
* While good enough for a prototype, having W|X pages in your program for an extended period
of time violates Data Execution Prevention (DEP/NX) and is generally a bad idea.
- The prototype only targets x86-64 Linux.
$ uname -a
Linux ubuntu-amd64 6.12.5-orbstack-00287-gf8da5d508983 # ...- The code uses APIs only available in Java 23+, though with some changes you can get it to work on Java 21 as well.
$ java -version
openjdk version "23.0.1" 2024-10-15
OpenJDK Runtime Environment Zulu23.30+13-CA (build 23.0.1+11)
OpenJDK 64-Bit Server VM Zulu23.30+13-CA (build 23.0.1+11, mixed mode, sharing)- Compile:
$ javac Demo.java- Run:
$ java --enable-native-access=ALL-UNNAMED DemoThis prototype utilizes a new, experimental feature that is still under evaluation. The method used here may not be reliable, and unexpected behavior or failures could occur. The author disclaims any responsibility for potential damage, data loss, or unintended consequences resulting from its use. Use at your own risk and exercise caution when deploying on critical environments.