Introduction to MinJreToolkit
1. Main goal
The main goal of MinJreToolkit is to implement a set of tools to help developers decide which files of a JRE are necessary to execute a certain custom JAVA application and then reduce the JRE size as much as possible to just suit the custom application.
2. General idea
The general idea of MinJreToolkit is to implement tools to automatically analyze the custom application in several different ways and define a uniformed result format which can be merged easily for all these tools. The analyzing strategies include constant pool parsing, dynamic tracing and so on.
Due to the possible dynamic behavior of JAVA application, it is hard to strictly decide the exact necessary files given a JRE implementation and a JAVA application. Therefore, the task of the MinJreToolkit is to reduce the JRE to a size which will be very possibly larger than the real minimum size a bit.
3. Detailed design
In the current version of MinJreToolkit, the basic result format has been defined, associated with a class which contains the data of result and the operations between results. Besides, two analyzing strategies and a JRE generator have been implemented to perform static and dynamic analysis to a custom application and generate a new JRE from an origin one according to the final merged result.
The basic result format
The current result file is a plain text file with a cns file name extension, called CNS (Class Name Set) file. The format of CNS file is simple. It must start with a line of “Class Name Set File” and followed by a list of fully qualified class name (such as java.io.File) or package name (such as java.lang.*), only one item in a line without any empty characters before and after the name. Empty lines are allowed between name lines for that sometimes the user may want to organize the names in groups.
The associated class, called ClassNameSet, is the data structure that contains and manages the class name set. It can be initialized from a cns file and can also be output to a cns file. More, it can merge from a cns file or another instance of ClassNameSet. When trying to add a class name or a package name, the instance of ClassNameSet will examine the name to decide whether it is a valid name. If not, it will ignore it quietly and continue. The future version of MinJreToolkit may add this kind of exception information to a log file.
The static analysis is implemented based on bcel 5.2. The analyzer parses the constant pool of each class with bcel, get the dependence between classes and employ a simple dependency closure algorithm to calculate the closure. The output of this analyzer is a cns file containing JRE classes in the closure.
The dynamic analysis is implemented as a JVMTI agent. It parses all the class load events taking place in the runtime of an application and give a cns file output. The agent is supposed to be used in tests. The coverage of this kind of dynamic tracing depends on the coverage of test cases.
The JRE generator should be initialized by a cns file which contains the necessary classes that a certain JRE implementation must load in any case. Then cns files can be added to the generator. After that, the generator copy necessary files and “reduced” jar files to the target JRE directory.
4. What can you do with the code
Currently the code contains
- the functional module of static analyzer and JRE generator
- the main class of static analyzer and JRE generator
- the command line scripts of static analyzer and JRE generator (in Windows environment)
- the Ant task of static analyzer and JRE generator
the dynamic tracing agent (in both Windows & Linux environment)
A build.xml file has been provided to compile, test both command line version and Ant task version of static analyzer and JRE generator as well as the dynamic tracing agent and finally create the distribution of MinJreToolkit including binary files, command line scripts and documentation.
To execute the build.xml file, the user should have Apache Ant installed and well configured. Before the execution, the property of “jdk.dir”, “origin.dir”, “target.dir” should be correctly configured to your JDK directory, the original JRE directory and the target JRE directory respectively. More, ant.jar & bcel-5.2.jar should be downloaded and included in the “lib” directory first. To download these two jar files, please refer to the following link:
Apache Ant: http://ant.apache.org/
BCEL of Apache Jakarta: http://jakarta.apache.org/bcel/index.html
Run “ant” in the demo directory will execute the default target “dist” and get a distribution of MinJreToolkit in the “dist” directory after compiling and testing. After that, the user can use the distribution to help him/her get a reduced JRE for his/her own application.
Run “ant clean” in the demo directory will clean all files possibly produced in the previous building process and restore the demo directory to the original status.
5. Future plan
Currently the following improvements are in the future plan:
- Optimize the closure calculation algorithm used in the static analysis
- Reduce the output closure size
- Enhance the performance of the algorithm in speed and memory occupancy
- Add other analyzers to make the toolkit generate smaller JRE
- Add a log functional module to the toolkit to provide more information to user
6. Contact info
If you find any bug or have any suggestion, please feel free to contact me mailto:firstname.lastname@example.org.