Java Reusable Tools-JAR Package

Source: Internet
Author: User

Objective

Java provides a mechanism for jar packages so that the already developed classes can be successfully reused in future projects.

This chapter focuses on how to use this tool.

The role of the package

Packages can organize classes of different functions to ensure the uniqueness of class names.

To ensure the uniqueness of the package name, Sun recommends that the package publishing company's Internet domain name be reversed as the directory containing the package, and that different projects use different sub-packages.

The package structure is typically a multi-level directory with various. class files.

However, in the actual project, the package compression format-jar package is generally used.

Packaging methods

Here is an example of a complete packaged jar package.

1. Create a new Java project.

2. Add various classes in the package. Remember to enter the appropriate package name in the Create dialog box at the new class:

  

2. You can see that the source code directory structure of Eclipse has changed-added a level pack directory and a new class file in the package directory (A.class test class for this article):

  

3. Write your class. My test case code is as follows:

1  Packagefangmeng.com.cnblogs.www;2 3  Public classA {4 5     //define a static constant field pi6      Public Static Final DoublePI = 3.1415926;7     8     //then define a static method to display pi9      Public Static voidshowpi () {Ten System.out.println (PI); One     } A}

4. Right-click Package name, Export, and then select the Jar File in the new dialog box

  

5. Under the popup dialog box, enter the output jar package name:

  

6. Then always "next" to complete. The path and file name of the newly generated jar package is the one specified in the previous step.

Ways to use Packages

1. Right-click Project--Properties, the dialog box on the left of build Path, to the right libraries, as shown in:

  

2. Click Add External JARS on the right to select the jar package that you just generated:

  

3. You can see that the project directory has more new imported jar packages and their internal classes, configuration files, etc.:

  

4. In the source code, the header joins the corresponding import statement to import the specified Java package, class, and reuse in the current project. The test code is as follows:

1  Packagetest;2 3 //Import a specified child package within a jar package4 Importfangmeng.com.cnblogs.www.*;5 6  Public classTest {7      Public Static voidMain (string[] args) {8         9         //directly using the method inside the jar packageTen A.SHOWPI (); One     } A}

5. The execution results are normal, such as:

  

Summary

This article gives only one of the most classic methods for encapsulating the use of jar packages.

When the actual project encapsulates the jar package, it is necessary to add resources to the jar package, write the configuration file, set the main function, and so on, this is a regular, qualified jar package.

  

Java Reusable Tools-JAR Package

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.