Sunday, May 28, 2017

Introduction to Java programming, Part-1 (13)

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

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

The JDK is complete, yet it doesn't do all that you requirement for composing awesome Java code. As you develop more OK with composing Java applications, you might need to utilize increasingly outsider applications to bolster your code. The Java open source group gives numerous libraries to help shore up these holes. 

Assume, for instance, that you need to utilize Apache Commons Lang, a JDK swap library for controlling the center Java classes. The classes given by Commons Lang help you control exhibits, make arbitrary numbers, and perform string control. 

How about we expect you've as of now downloaded Commons Lang, which is put away in a JAR record. To utilize the classes, your initial step is to make a lib catalog in your venture and drop the JAR document into it:
  1. Right-click the Intro root folder in the Eclipse 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. In Package Explorer, select the lib folder, right-click, and select Refresh.
  2. Verify that the JAR shows up in the lib folder
  3. Right-click commons-lang3-3.4 and choose Build Path > Add to Build Path.

After Eclipse processes the code (that is, the class files) in the JAR file, they're available to reference (import) from your Java code. Notice in Project Explorer that you have a new folder called Referenced Libraries that contains the commons-lang3-3.4.jar file.
Previous Post
Next Post

post written by:

0 coment�rios: