A Preliminary Study on the idea of streamlining JRE

Source: Internet
Author: User

Introduction: JRE is the basic environment for running Java programs. Currently, JRE is very large. Even to run a simple hello World Program, it may still need to rely on the entire JRE, nearly MB of dependency. Can I streamline the JRE that a specific Java program depends on? Of course, yes. Based on the needs of the current Code, the JRE can be dynamically streamlined, relying only on the required class, not all.

1. Overall Thinking

A. First, find all the classes that the current Java program depends on, including its own class libraries/third-party class libraries, and the class libraries in JRE.

B. Remove unnecessary class library files from the JRE and retain only the required class libraries.

C. repackage the reserved class library and replace the existing JRE file.

2. Find the class library file in the required JRE.

When running a Java application, you can add the parameter [-XX: + traceclassloading] To the JVM. During the startup process, the application prints all required classes to the console.

In the preceding example, all dependent class libraries are listed.

2. How to extract the required class library or remove the unnecessary class library

Here we use the former to extract only the required class libraries.

jar xvf xxx.jar classname1 classname2 ....
This command extracts the required class from the jar and copies it to the local directory.

3. Package these class libraries and replace them with the corresponding class libraries in the JRE.

jar cvf target.jar sourcefolder1 classfolder2 ...
Package command to package the class library in classfolder as target. jar.

4. How can I use the above Code to automate the packaging of the corresponding JRE class library?

The solution is as follows:

4.1. print out the Java class used in JRE Based on the Java parameter-XX: + traceclassloading in the running process

4.2. Capture the list of classes output from the console

4.3. Use the functions provided by jar to extract the required class from Rt. jar.

4.4. Package the class extracted from Rt. jar to obtain the required JRE core jar package.

Premise of code assumption:

1. JRE path

2. The target Java class has been compiled into a class. dynamic compilation is not considered here.

3. Package Rt. jar in JRE in the current path.

The sample code is as follows:

import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;import java.util.ArrayList;import java.util.List;public class RunClass {public static void main(String[] args) throws IOException {List<String> classes = new ArrayList<String>();String[] cmdB = { "java", "-XX:+TraceClassLoading", "MainTest" };Runtime runtime = Runtime.getRuntime();Process process = Runtime.getRuntime().exec(cmdB);// /process = Runtime.getRuntime().exec(cmdB);LineNumberReader br = new LineNumberReader(new InputStreamReader(process.getInputStream()));StringBuffer sb = new StringBuffer();String line;while ((line = br.readLine()) != null) {String className = RunClass.parseClass(line);if (className.length() > 0) {sb.append(className.replace(".", "/")).append(" ");classes.add(className.replace(".", "/"));}}System.out.println("classes to be packed in size:" + classes.size());classes.add(0, "/opt/jdk7/jre/lib/rt.jar");classes.add(0, "xvf");classes.add(0, "jar");Process jarClass = runtime.exec((String[]) classes.toArray(new String[classes.size()]));LineNumberReader br1 = new LineNumberReader(new InputStreamReader(jarClass.getInputStream()));StringBuffer sb1 = new StringBuffer();String line1;while ((line1 = br.readLine()) != null) {System.out.println("extracting:" + line1);}System.out.println(classes.size()+ " classes have been extracted successfully");String[] cmdJarPackage = { "jar", "cvf", "rt.jar", "com", "java","javax", "META-INF", "org", "sun", "sunw" };Process jarProcess = runtime.exec(cmdJarPackage);System.out.println("Jar the classes into a package rt.jar successfully.");}public static String parseClass(String lineStr) {String keyStr = "";if (lineStr.startsWith("[Loaded")) {String[] keys = lineStr.split(" ");if (keys.length == 4) {keyStr = keys[1];}}return keyStr;}}

5. Summary

JRE has been modularized in jdk8, making it possible to load and customize JRE on demand. Using the C method to directly call commands is not very flexible and controllable. For example, jarouputstream and jarinputstream can be used for packaging and decompression, which is more controllable and flexible.

   

A Preliminary Study on the idea of streamlining JRE

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.