Archiving Java code.

Now that you've learned a bit about writing Java applications, you might be wondering how to package them up so that other developers can use them, or how to import other developers' code into your applications. This section shows you how.

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
Screenshot of the Eclipse 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:
  1. Right-click the Intro root folder in Project Explorer view.
  2. Click New > Folder and call the folder lib.
  3. Click Finish.
The new folder shows up at the same level as src. Now copy the Commons Lang JAR file into your new lib directory. For this example, the file is called commons-lang3.3.4.jar. (It's common in naming a JAR file to include the version number, in this case 3.4.)
Now all you need to do is tell Eclipse to include the classes in the commons-lang3.3.4.jar file into your project:
  1. Right-click the Intro project in your workspace; then click Properties.
  2. In the Properties dialog box, click the Libraries tab, as shown in Figure 11:
    Figure 11. Properties > Java Build Path
    Path to select the Properties dialog box and Libraries tab.
  3. Click the Add External JARs button. Browse to the project's lib directory, click the commons-lang3.3.4.jar file, and click OK.
Once the code (that is, the class files) in the JAR file have been processed by Eclipse, they are available to reference (import) from your Java code. Notice in Project Explorer that there is a new Folder called Referenced Libraries that contains the commons-lang3.3.4.jar file.
Share on Google Plus

About M

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment