Extract Java classes information from AOT compiled binary file

3Keys
2 min readMay 16, 2021

AOT compilation is a topic that has been discussed in the Java world for a long time, but until GraalVM appeared, there was no tool that could achieve a better AOT compilation, let alone drive the whole ecosystem. Although GraalVM provides the native-image tool that can effectively help developers perform AOT compilation, it is still very difficult to achieve an out-of-the-box AOT compilation for Java applications due to the complexity and diversity of the Java ecosystem, especially since reflection and proxy technologies are widely used in various common frameworks.

Spring Native Project

Thanks to the Spring Native project from the Spring framework team, they can achieve a one-click AOT compilation of the project through Maven or Gradle plug-ins. Of course, it is currently only available for Spring Boot projects.

Use Sping Native to generate AOT compiled Java programs

Here is the official Spring Native example to generate an AOT-compiled Java application (test environment: Ubuntu 20.04, Docker 20.10.6)

git clone https://github.com/spring-projects-experimental/spring-native.gitcd spring-native/sample/petclinic-jpa./build.sh

After a relatively long compilation time and a lot of memory consumption, we can get the AOT compiled Java program in the target directory, which is a standalone binary and can run independently without any dependencies.

petclinic-jpa is the program compiled by AOT and can be run directly

Extract Java classes information from AOT compiled binary file

Java code protection has been a very difficult problem in the past, and AOT is also considered a solution for Java code protection, but unfortunately, many Java programs nowadays cannot be separated from the framework, and due to the complexity of the framework, even programs compiled by AOT have to include class information into the final generated binary file, and the class files are actually neatly arranged in the resources area of the binary file.

The following tool can scan and extract class information from AOT compiled binary files

git clone https://github.com/3-keys/binary-classfile-readercd binary-classfile-reader./gradlew run --args='<path-of-the-binary-file> <output-folder>'

Take the petclinic-jpa obtained in the previous step as an example, extract the class information from it, compare it with the source code, and you can see that Model information, Controller information, and Service information can all be obtained.

--

--