In Java, how do I encapsulate my own classes and create and use my own class libraries?

Source: Internet
Author: User
In Java, how can I encapsulate my own classes and create and use my own class libraries? -- Thank you for reading this article. For more information, see the source!

With the accumulation of programming experience, you will find that the classes you write in some projects are often used multiple times in other projects. You will surely think: How do you accumulate some common classes written by yourself and generate your own class libraries? If so, I'm glad you met someone in the channel, because I did the same. The following describes how I did it:

1. First understand the concepts of classpath and path.

PATH is the path of the program used for compiling and running in Java, and classpath is the path of the class. After installing JDK, you should set the path and classpath path. Now, assume that your JDK is installed in D: \ It \ jdk6.0 \ Java (TM) se Development Kit (because my own JDK is installed in this directory ), the path you want to set is the bin folder under this path, that is, D: \ It \ jdk6.0 \ Java (TM) se Development Kit \ bin, because the binfile folder contains javac.exeand java.exe (the program used for compiling and running in Java ). The method is as follows:

1. Open "my computer \ Advanced \ environment variable", if it is win7, it is: "computer \ properties \ System Properties \ Advanced environment variable", open 1,

Figure 1

Double-click the path under "system variables" and set D: \ It \ jdk6.0 \ Java (TM) se development kit.

Figure 2

Open the command prompt and enter javac. If there is a series of texts, the setting is successful. By the way, common command prompt: javac compiles java files, Java runs java files, Java-version queries JDK versions, package path + "-CVF" + classname. java into a jar package,

2. next, set classpath. If your Java file is in the E: Java folder, you can set it as follows: 3. The next one indicates the current path, in this way, you can use javac to compile Java files in any path.

Figure 3

3. If you want to use the classes in the jar package now, you need to set the jar package to the classpath variable. Suppose you have your own tools. jar package, placed in D: \ It \ jdk6.0 \ Java (TM) se Development Kit \ myjar, You Need To D: \ It \ jdk6.0 \ Java (TM) se Development Kit \ myjar \ tools. jar to classpath, 4

Figure 4

Now, you can reference the class in tools. jar in the class, just introduce it in the class. The above operations may still go wrong in win7, and you also need to set classpath in the user variable (why is this? I am not worth it, so I hope to discuss it together ). Note that in Figure 1 There are "adiministrator user variables" and "system variables". You only need to set D: \ It \ jdk6.0 \ Java (TM) according to Figure 4) se Development Kit \ myjar \ tools. set jar to classpath in user variables.

2. How to pack a jar package in eclipse? Generate your own class library

Eclipse and myeclipse are currently the most popular Java integrated development environment. If you are using other development tools, we suggest you try to use eclipse for development, because it is very powerful, easy to use. The following describes how to pack the classes you have written into a jar package in eclipse to generate your own class libraries.

Assume that the current document structure is as follows:

Figure 5

Right-click the tool of the project file and select export. In the displayed dialog box, select Java \ JAR file and click Next. If you only need to package cn.edu. jxau. you only need to select the class under tools.

Figure 6

In JAR file, select the path and file name to export. Figure 7

Figure 7

Click "Next" and "Next", select the main function (if there is a facial function) in the main class, and click "finish ).

3. Generate a javadoc file for your class.

In the second section, you can generate your own class libraries. However, classes and Methods written by myself will be forgotten for a long time, and it is necessary to generate a high-quality annotation document. Fortunately, Java can complete this function well and help us generate documentation similar to API, that is, javadoc documentation. The following describes how to generate javadoc documents in Eclipse:

1, Annotate a class

Before generating the javadoc document, you must first annotate the class. There are three methods of annotation:

A, // comment content; B,/* Comment content */; (shortcut key: select the content to comment and press "Shift + Ctrl + /", the uncomment is "Shift + Ctrl + \")

C,

/**

* Comment content.

*/

Only the C method can be used to generate javadoc. Take the arrayqueue class I wrote as an example to describe several annotations. You only need to comment on the public method and variable, because only the public type can be accessed by other classes.

(1) comment out the class description and creator before the class, such

/**

* Copy the file, input the string to the file, and print the file content to the console

*

*@ AuthorXiaoxu

*

*/

(2) comment out the description of the method before the public method, as shown in figurePublic staticString readfile (File file) method note:

/**

* Read the content of a file.

*

*@ ParamFile

* File to be read

*@ ReturnReturns the string of the object to be read.

*/

(3) Comment on the public variable. Suppose there is a variable that represents the window width.Width, As follows:

/**

* Width of the Main Window

*/

Public static final int Width= 1000;

Some results are as follows:

Figure 8 (1)

2. Use eclipse to generate javadoc documents

Select the project for which you want to generate the javadoc document, and then select project \ generate in the menu bar.
Javadoc .... After the dialog box is opened, the default path of "javadoc command" is c: \ JDK \ bin \ javadoc.exe. If this path is not available, add it by yourself, find the bin \ javadoc.exe under the root directory where JDK is installed, as shown in figure

Figure 8 (2)

Select the file to generate the javadoc file and select the export path. Generally, select the doc folder under the default project and change it to another path. As follows:

Figure 8 (3)

Next, click Finish. A warning may appear during this period, as shown in figure


Figure 8 (4)

Select "Yes to all ".

4. How to use a self-created class library?

You can create your own class libraries through steps 1, 2, and 3. The following describes how to use your own class library.

Method 1:

Assume that you want to create a project test, which uses the S Class in your own class library tools. jar. Create a new class test. Java with the main method as follows:

Public static voidMain (string [] ARGs ){

S.PL("Java"); // S. pl () in the S class encapsulates system. Out. println (OBJ );

}

At this time, eclipse will report an error, saying that the class S does not exist and whether to create this class. This is because the default JRE does not have the tool. jar package, which is 8.

Figure 8 (5)

So how can we use the default JRE system library of this package channel?

Select window \ preferences. In the displayed dialog box, select Java \ Installed jres.

Figure 9

Next, click "add" to add a new JRE, select standard Vm, and click Next. in JRE home, click directory and select the JRE folder (including RT) under the JDK button path. jar folder, Rt. jar is the JDK root folder, which contains all the basic classes), and then click "add external jars" to select the generated Tools. JAR file (it is best not to use tools first. jar files are placed in a secure and relatively accessible folder. My files are directly placed in the JDK installation path D: \ It \ jdk6.0 \ Java (TM)
Se Development Kit \ myjar), 10

Figure 10

Click Finish.

In the test project, replace the default JRE system library with the configured JRE, right-click test, select build path, and select configure build path ..., Open the dialog box, remove the original JRE, add library, select JRE system library, next, click "installed jres", select the configured JRE, and click Finish.

Now the configuration is complete. In this case, the JRE can see the tools. jar file, for example:

Figure 11

Method 2:

Replace the generated class library tool. jar is directly copied to JRE/lib/EXT/In the JDK and JRE installation directories. At this time, we need to restart eclipse, in this way, the package is loaded into the Automatically Generated System Library.

Method 3:

Of course, in addition to the above methods, you can also directly import external jar and additional jar packages in a single project through "bulid path. This method is similar to replacing a new JRE. Let's figure it out! Haha ......

Of course, this is just a method I have figured out, and there may be better methods. I still need to learn more about this. If you are interested in this aspect, join me and learn from each other. My contact method is as follows:

Email: luoweifu@126.com;

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.