Sort out JRE slimming or streamlining JRE

Source: Internet
Author: User

Not long ago, I made a desktop program for my friend. The program file is not big, but JRE is required to run Java programs. However, JRE has over 80 m, then I searched the internet for how to slim down or streamline the JRE, as shown below:

Open the JRE installation directory. The directory contains the bin and Lib folders, so the two files are reduced,

1. Bin: it can be considered as a Java virtual machine.

2. Lib: Class Libraries and resource files required by the Java Virtual Machine to execute class files.

 

I. Bin slimming mainly involves two aspects

① EXE file. The most important tool is java.exe, which is used to execute class files. If it is only for Java program running, other executable files are generally not used (can be excluded ).

② The DLL file is used in the process of executing the class file by java.exe. the class file is executed, and the DLL is loaded by the library file required by java.exe, which can be removed if not needed.

What we need to do is find which DLL files are useful? Let's run a Java file to see if we can use 360 security guard to get

1. Prepare the Java file:

/* @ Author jarg @ todo example to view the DLL file required by the current program */import Java. io. inputstreamreader; import Java. io. ioexception; public class Hello {public static void main (string [] ARGs) throws ioexception {inputstreamreader IR = new inputstreamreader (system. in); system. out. println ("hello"); IR. read ();}}

2. Compile and run

3. 360 security guard-> function overview-> display in the upper right corner of the Process Manager loaded to the DLL in the currently selected Process


In this example, we can leave java.exe, useful DLL files, and client directories.


Bin slimming is successful here!

Ii. Lib slimming

① The main class library in the lib directory is Rt. jar, which is required by any Java program.
The lib directory is about 62 MB, but the Rt. Jar Class Library occupies 47 MB. It can be seen that the simplified bin directory is mainly used to crop Rt. jar.

 

② Global variables of the dynamic link library are used to load the corresponding dynamic link library files.

 

③ The lib directory contains not only the class libraries and configuration files required for running the program, but also system resources such as mouse cursor and font. simple programs can remove these resources if they are not used. if the program removes the JRE part and occupies a large amount of space, we recommend that you keep the configuration file content less than 20 mb to avoid the trouble caused by resource loading errors.


The main steps are as follows:

1. Extract the required class library (jar) and use the-verbose command to view all classes loaded by the virtual machine when running the Java program, such:

@echo offC:/Java/jdk1.6.0_16/bin/java -jar  -classpath lib/*.jar; -verbose:class printSoft.jar >> class.txtpause

Save the following information in the class.txt file:

[Loaded java.lang.Math from shared objects file][Loaded java.nio.charset.Charset$3 from C:\Java\jdk1.6.0_16\jre\lib\rt.jar][Opened C:\Java\jdk1.6.0_16\jre\lib\charsets.jar][Loaded sun.nio.cs.AbstractCharsetProvider from C:\Java\jdk1.6.0_16\jre\lib\rt.jar][Loaded sun.nio.cs.ext.ExtendedCharsets from C:\Java\jdk1.6.0_16\jre\lib\charsets.jar][Loaded java.lang.Class$1 from shared objects file][Loaded sun.reflect.ReflectionFactory$1 from shared objects file][Loaded sun.reflect.NativeConstructorAccessorImpl from shared objects file]

We can get the jar files and class files we need from class.txt. It is very easy to submit the jar files. I will not talk about it. Next we will look at how to submit the class files we use:

Since the class.txt lines are the same: [loaded Java. lang. system from shared objects file] is a string of characters. You can modify the text to obtain the complete class name Java. lang. to obtain a string of characters similar to the class path Java/lang/system.
Modification method:
1. Search for and replace [loaded is empty to delete [loaded.
2. Use any text editor with the regular expression search and replacement function to search for and replace from. * with null values to delete the from and Its strings.
3. Search for and replace it/
4. Delete rows starting with [opened.
5. Delete the output line of system. Out. println in the program.
After extraction, class.txt contains the following information:

java/lang/Objectjava/io/Serializablejava/lang/Comparablejava/lang/CharSequencejava/lang/Stringjava/lang/reflect/GenericDeclaration.......


2. Extract the class file from the existing jar package, package it into a jar, and finally replace the original jar. Below is a tool class for extracting the class:

Import Java. io. bufferedreader; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstreamreader; public class copyclass {private string source = "C: \ Users \ lzp \ Desktop \ printsoft \ jre6 \ Lib \\"; // class source directory private string DEST = "C: \ Users \ lzp \ Desktop \ printsoft \ jre6 \ Lib \\"; // copy the target directory string [] jararr = new string [] {"RT", "charsets "}; /***** @ Param source class source directory * @ Param DEST class copy target directory * @ Param jararr needs to extract jar files */Public copyclass (string source, string DEST, string [] jararr) {This. source = source; this. deST = DEST; this. jararr = jararr;} public static void main (string [] ARGs) {string [] jararr = new string [] {"RT", "charsets "}; copyclass OBJ = new copyclass ("C: \ Users \ lzp \ Desktop \ printsoft \ jre6 \ Lib \\",
"C: \ Users \ lzp \ Desktop \ printsoft \ jre6 \ Lib \", jararr); obj. readandcopy ("C: \ Users \ lzp \ Desktop \ printsoft \ class.txt ");} /***** @ Param LOGNAME extract class details */Public void readandcopy (string LOGNAME) {int COUNT = 0; // Number of classes used to record successful copies try {fileinputstream Fi = new fileinputstream (LOGNAME); inputstreamreader IR = new inputstreamreader (FI); bufferedreader BR = new bufferedreader (IR ); string string = BR. readline (); While (string! = NULL) {If (copyclass (string) = true) Count ++; elsesystem. out. println ("error" + Count + ":" + String); string = BR. readline () ;}} catch (ioexception e) {system. out. println ("error:" + E);} system. out. println ("count:" + count);}/***** extract the corresponding class from the original jar path to the target path, for example, extract the Java/lang/charsequence class from the RT directory to the rt1 directory * @ Param string extraction class full path * @ return * @ throws ioexception */Public Boolean copyclass (string) throws ioexcept Ion {string classdir = string. substring (0, String. lastindexof ("/"); string classname = string. substring (string. lastindexof ("/") + 1, String. length () + ". class "; boolean result = false; For (string jar: jararr) {file srcfile = new file (source + "/" + jar + "/" + classdir + "/" + classname); If (! Srcfile. exists () {continue;} byte Buf [] = new byte [256]; fileinputstream fin = new fileinputstream (srcfile);/* the target directory does not exist, create */file destdir = new file (DEST + "/" + jar + "1/" + classdir); If (! Destdir. exists () destdir. mkdirs (); file destfile = new file (destdir + "/" + classname); fileoutputstream fout = new fileoutputstream (destfile); int Len = 0; while (LEN = fin. read (BUF ))! =-1) {fout. Write (BUF, 0, Len);} fout. Flush (); Result = true; break;} return result ;}}

Then, package the extracted class file into a jar file, use the jar command to package it, and then replace the previous JAR file, so that the Lib will be from more than 60 m to several M,



In this way, we have completed JRE slimming!

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.