Wednesday, 21 March 2012

Create your own library package in Java

Today I am going to post on how you can create your own package and can import it from anywhere just like yo use import* .For this you need to do following things :
1 : Since you are thinking about package so while writing your source files write
 package <packagename> without <> . For each of your subpackages write source files in subfolders and give proper package name. e.g. creating Paint do this
     package Draw for Draw class in Draw Folder. Then create a sub folder 2DShapes containing Rectangle and Circle source codes having first line as
    package Draw.2DShapes
 2 : Next you will have to gather all your class files in a similar way as your source. To gather them all separately folder by folder use the -d command while compiling at command prompt. Type the following code
   javac -d ../classes if you are running cmd from inside Draw folder. But remember to create classes folder in same directory as that of Draw folder.
3 : Next assemble all these classes into a JAR file without a manifest file using -cvf command.
4  ; Next open the Java folder where you have installed Java. Then look for the following folders subsequently.I mean look for
  C:\Program Files\Java\jdk1.7.0\jre\lib\ext . Inside ext folder paste your JAR file.
That's It. Now you can use your own Draw package by importing from anywhere while writing your source codes. This method is known as Java Extension Mechanism. This method is followed while developing projects using Third-Party library functions.

No comments:

Post a Comment