On the idea of simplifying JRE

Source: Internet
Author: User
Tags java reference

Introduction: The JRE is the basic environment on which Java programs are implemented, and the JRE is now very large, even in order to execute a simple Hello World program. May still need to rely on the entire JRE, near the hundred-gigabit-size dependency.

Is it enough to streamline the JRE that a particular Java program relies on? Of course it can. Dynamically streamline the JRE based on the needs of the current code, relying only on the required class. Rather than all.

1. General Ideas

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

B. Remove the unwanted class library files from the JRE, preserving only the required class libraries.

C. Package the reserved class library once again. Replacing an existing JRE file

2. Find the class library file in the desired JRE

In the process of executing a Java application, you can add a reference [-xx:+traceclassloading] to the JVM, and the application will print all the required classes to the console during the startup process.

In the example above, the fully dependent class library is listed.

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

Here we use the former, only to extract the required class library.

Jar XVF Xxx.jar classname1 classname2 ....
This command extracts the required class from the jar and copies it to the local current folder.

3. Package These class libraries to replace the corresponding class libraries in the JRE

Jar CVF Target.jar sourcefolder1 classfolder2 ...
Package the command to package the class library in Classfolder as Target.jar.

4. What about the code? Use the above only to complete their own initiative to package the corresponding library of JRE it?

Scenarios such as the following:

4.1. Print out the Java classes used in the JRE based on the Java reference-xx:+traceclassloading in the implementation process

4.2. Capturing the class list output from the console

4.3. Take advantage of the functionality that comes with the jar. Extract the required class from the Rt.jar

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

The premise of the code assumption:

1. The path where the JRE resides

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

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

The demo 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 () &G T 0) {sb.append (Classname.replace (".", "/")). Append (""); Classes.add (Classname.replace (".", "/"));}} System.out.println ("classes to being 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 has 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 = Li Nestr.split (""); if (keys.length = = 4) {keystr = keys[1];}} return keystr;}}

5. Summary

The JRE has been modularized in jDK8, making it possible to load and customize the JRE on-demand. The demo sample code here is simply indicative of the process, there is a big gap from the real tool, the basic reason is a large number of use of the Runtime.exec method to invoke the command directly, which is not very flexible and controllable, for example, packaging and decompression can use Jarouputstream, Jarinputstream and so on. will be more controllable and flexible.

   

On the idea of simplifying 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.