J2SE synthesis: A brief talk on the JRE of Java program release

Source: Internet
Author: User
Tags command line parent directory

Java Development Program, the issue always need to consider is how to upload the JRE on the user's machine. There are many issues to consider: whether the user has the ability to install the JRE by themselves, whether the user already has a JRE and the version we need is inconsistent, if there is a version problem, and so on. Use. NET, there are fewer issues to consider. Right now. The NET CLR seems to have become popular, and bullish version D of Win XP will install the latest on its own. NET CLR, and it seems that its installation interface is also friendlier than the JRE. The solution to the problem of installing the JRE is to get our application to carry the jre! In this way, our program is like a traditional Win32 application, double-click can be executed, regardless of whether there is a JRE on the machine, what version of the JRE, no matter what, I have my own! To do that, it's actually very easy.

Sen Wang's first chapter in his "Java Depth Adventures" (a strong recommendation for this book, with fewer content) explains the relationship between JDK,JRE,JVM. Explain what happened when we performed the Java.exe. It mentioned that java.exe in accordance with a set of logic to find the JRE can be used, first of all find their own directory there is no JRE (according to Sen Wang said this is not accurate, I do not have all the source code JDK, this can not be verified); second, find your own parent directory under no JRE , and finally the registry that queries Windows.

Usually we can perform java.exe on any directory on the machine where the JRE is installed. Because it was copied to the Windows system32 directory at the time of installation, which would be in the PATH environment variable anyway. The Java.exe will eventually have access to the registry to determine the location of the real JRE. If we require each application to bring its own JRE, this path must not be taken. However, the second rule of logic says that Java.exe will look for the JRE in its parent directory, and the solution is in this section.

Let's say our app is packed, called Myapp.jar, and placed in the MYAPP directory. Can we execute Java in the MyApp directory? Jar Myapp.jar to run our program. We are installing JRE 1.5, under C:\Program files\java\jre1.5.0. Now, we simply need to move the jre1.5.0 directory to the MyApp directory, by the way of an easy to write name such as JRE. Now, our application is like this:

MyApp

Myapp.jar

Jre

All content under the Jre1.5.0 directory

Java.exe is in the bin directory in the JRE directory. According to the second logic, Java.exe looks for the JRE in its parent directory, and the experiment confirms that it looks for the Lib directory, and Lib is in the JRE directory. So Java.exe will determine where the JRE is and then execute the Java program normally, without going through whether we have the JRE installed, or if there are any registry entries in the registry.

Try to enter the MyApp directory under the command line and, assuming it's in C, point the path to the JRE under MyApp:

Set Path=c:\myapp\jre\bin

And then run:

Java? Verbose? Jar Myapp.jar

Add the verbose parameter to make sure we did use this set of JRE that was moved out of the home.

The program can run, and in the first few lines of the output from the command line, you can see:

[Opened C:\MyApp\jre\lib\rt.jar]

[Opened C:\MyApp\jre\lib\jsse.jar]

[Opened C:\MyApp\jre\lib\jce.jar]

[Opened C:\MyApp\jre\lib\charsets.jar]

So the program reads indeed its private JRE.

At this point, we seem to have completed the task. But now our private JRE is still imperfect, and the downside is too great. JRE 1.5 is close to 70MB, and as our private JRE, a lot of content can be discarded. JRE directory under the license can not, the bin under the execution of files only need to retain Java.exe or javaw.exe,lib, as long as the retention of Rt,jsse, jce,charsets a few libraries on it. In addition to the i386 and Zi two subdirectories, the rest of the subdirectories are not available. Zi only need to keep a subdirectory of its own area and some of the files under it. LIB is reserved for property files other than libraries. So clean up, the JRE is still close to 50MB. You can also continue to clean up a few library files do not need content, which requires careful collation, it will be very time-consuming. It's best to write an automated tool to help us organize them. The Java-written media player from Sun's JMF comes with a JRE and only a few MB.

After cleanup, we need to run our application several times to make sure our JRE is not missing anything.

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.