JARs
The JDK ships with a tool called JAR, which stands for Java Archive. You use this tool to create JAR files. After you package your code into a JAR file, other developers can drop the JAR file into their projects and configure their projects to use your code.Creating a JAR file in Eclipse is easy. In your workspace, right-click the
com.makotojava.intro
package and click File >
Export. You see the dialog box shown in Figure 10. Choose Java
> JAR file and click Next. Figure 10. Export dialog box
When the next dialog box opens, browse to the location where you want to store your JAR file and name the file whatever you like. The .jar extension is the default, which I recommend using. Click Finish.You see your JAR file in the location you selected. You can use the classes in it from your code if you put the JAR in your build path in Eclipse. Doing that is easy, too, as you see next.
Using third-party applications
As you grow more comfortable with writing Java applications, you might want to use more and more third-party applications to support your code. As great as the JDK is, it does not do everything you need for writing great Java code. The Java open source community provides many libraries to help shore up these gaps. For the sake of example, suppose that you want to use Commons Lang, a JDK replacement library for manipulating the core Java classes. The classes provided by Commons Lang help you manipulate arrays, create random numbers, and perform string manipulation.Let's assume you've already downloaded Commons Lang, which is stored in a JAR file. To use the classes, your first step is to create a lib directory in your project and drop the JAR file into it:
- Right-click the Intro root folder in Project Explorer view.
- Click New > Folder and call the folder
lib
. - Click Finish.
Now all you need to do is tell Eclipse to include the classes in the commons-lang3.3.4.jar file into your project:
- Right-click the Intro project in your workspace; then click Properties.
- In the Properties dialog box, click the Libraries tab, as shown in Figure 11:
Figure 11. Properties > Java Build Path
- Click the Add External JARs button. Browse to the project's lib directory, click the commons-lang3.3.4.jar file, and click OK.
0 comments:
Post a Comment