Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with Libraries

Similar presentations


Presentation on theme: "Working with Libraries"— Presentation transcript:

1 Working with Libraries
UNIT - V Working with Libraries

2 Creating Java Library JARs
Create a JAR-based library that accesses only Java 5 (and earlier) APIs via JDK command-line tools or Eclipse. Suppose you plan to create a simple library of math-oriented utilities. This library will consist of a single MathUtils class with various static methods. // MathUtils.javapublic class MathUtils { public static long factorial(long n) if (n <= 0) return 1; else return n*factorial(n-1); } }

3 MathUtils currently consists of a single static factorial() method for computing and returning factorials (perhaps for use in calculating permutations and combinations). You might eventually expand this class to support fast Fourier transforms and other math operations not supported by the java.lang.Math class. To Developing a JAR-based library with the JDK is trivial. Perform the following steps to create a mathutils.jar file that contains the MathUtils class:

4 1. start Eclipse IDE if not already running. 2
1. start Eclipse IDE if not already running. 2. Select New from the File menu and Java Project from the resulting popup menu. 3. On the resulting New Java Project dialog box, enter mathutils into the Project name textfield and click the Finish button. 4. Expand Package Explorer’s mathutils node. 5. Right-click the src node (underneath mathutils) and select New, followed by Package from the resulting pop-up menus. 6. On the resulting New Java Package dialog box, enter com.sjc.mathutils into the Name field and click Finish. 7. Right-click the resulting com.sjc.mathutils node and select New, followed by Class on the resulting pop-up menus.

5 8. On the resulting New Java Class dialog box, enter MathUtils into the Name field and click Finish. 9. Type your Code then save. 10. Right-click the mathutils project node and select Build Project from the resulting pop-up menu. (You might have to deselect Build Automatically from the project menu first.) 11. Right-click the mathutils project node and select Export from the resulting pop-up menu. 12. On the resulting Export dialog box, select JAR file under the Java node and click the Next button. 13. On the resulting JAR Export pane, keep the defaults but enter mathutils.jar in the JAR file textfield. Click Finish. The resulting mathutils.jar file is created in your Eclipse workspace’s root directory.

6 Using Java Library JARs
You’ve successfully built mathutils.jar and want to learn how to integrate this JAR file into your Eclipse-based Android projects. You’ll need an Android app to try out this library Use your platform’s file manager program (such as Windows XP’s Windows Explorer) to select and drag the previously created mathutils.jar file to the libs node available on your Application folder Right-click mathutils.jar and select Build Path followed by Configure Build Path on the resulting pop-up menus. On the resulting Properties for UseMathUtils dialog box, select the Libraries tab and click the Add Jars button. On the resulting JAR Selection dialog box, expand the UseMathUtils node followed by the libs node. Select mathutils.jar and click OK to close JAR Selection. Click OK a second time to close Properties for your Application


Download ppt "Working with Libraries"

Similar presentations


Ads by Google