Saturday, 22 August 2009

How to create Custom Java Packages Using Net Beans

By Ali Riza SARAL

This document will explain how to create custom packages with NetBeans. You can create your own java packages and put them in jar files so that you can call or use java objects from them.

This document is based on the example Geometry.java example from Wrox - Beginning Java 2 JDK5 Edition -2005 by Ivor HORTON which can be found on the internet.

The solution is given in stages, so please be patient and try to understand each stage as quick as possible:

1. STAGE:

There is only one package and that is the main program’s package named PackageEx.

----------------------------------------------------
SOURCE of packageex.java
----------------------------------------------------
package packageex;

class Line {
Point start; // Start point of line
Point end; // End point of line

// Create a line from two points
Line(final Point start, final Point end) {
this.start = new Point(start);
this.end = new Point(end);
}

// Create a line from two coordinate pairs
Line(double xStart, double yStart, double xEnd, double yEnd) {
start = new Point(xStart, yStart); // Create the start point
end = new Point(xEnd, yEnd); // Create the end point
}

// Calculate the length of a line
double length() {
return start.distance(end); // Use the method from the Point class
}

// Convert a line to a string
public String toString() {
return "(" + start+ "):(" + end + ")"; // As "(start):(end)"
} // that is, "(x1, y1):(x2, y2)"

// Return a point as the intersection of two lines -- called from a Line object
Point intersects(final Line line1) {
Point localPoint = new Point(0, 0);

double num =
(this.end.y - this.start.y)*(this.start.x - line1.start.x) -
(this.end.x - this.start.x)*(this.start.y - line1.start.y);

double denom =
(this.end.y - this.start.y)*(line1.end.x - line1.start.x) -
(this.end.x - this.start.x)*(line1.end.y - line1.start.y);

localPoint.x = line1.start.x + (line1.end.x - line1.start.x)*num/denom;
localPoint.y = line1.start.y + (line1.end.y - line1.start.y)*num/denom;

return localPoint;
}
}
----------------------------------------------------------
SOURCE of point.java
-----------------------------------------------------------
package packageex;

class Point {
// Coordinates of the point
double x;
double y;

// Create a point from coordinates
Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}

// Create a point from another Point object
Point(final Point oldPoint) {
x = oldPoint.x; // Copy x coordinate
y = oldPoint.y; // Copy y coordinate
}

// Move a point
void move(double xDelta, double yDelta) {
// Parameter values are increments to the current coordinates
x += xDelta;
y += yDelta;
}

// Calculate the distance to another point
double distance(final Point aPoint) {
return Math.sqrt(
(x - aPoint.x)*(x - aPoint.x) + (y - aPoint.y)*(y - aPoint.y) );
}

// Convert a point to a string
public String toString() {
return Double.toString(x) + ", " + y; // As "x, y"
}
}
-----------------------------------------------------
SOURCE of TryGeometry.java
-----------------------------------------------------
package packageex;

public class TryGeometry {
public static void main(String[] args) {
// Create two points and display them
Point start = new Point(0.0, 1.0);
Point end = new Point(5.0, 6.0);
System.out.println("Points created are " + start + " and " + end);


// Create two lines and display them
Line line1 = new Line(start, end);
Line line2 = new Line(0.0, 3.0, 3.0, 0.0);
System.out.println("Lines created are " + line1 + " and " + line2);

// Display the intersection
System.out.println("Intersection is " + line2.intersects(line1));

// Now move the end point of line1 and show the new intersection
end.move(1.0, -5.0);
System.out.println("Intersection is " + line1.intersects(line2));
}
}
-----------------------------------------------------------
2. STAGE:

There is an additional package named com.ars.geometry besides the packageex2 which belongs to the main program. Please be aware of the differences in the package statements at the top of each program.
TryGeometry has a import statement also…


-----------------------------------------
SOURCE of Line.java
-----------------------------------------
package com.ars.geometry;

public class Line {
Point start; // Start point of line
Point end; // End point of line

// Create a line from two points
public Line(final Point start, final Point end) {
this.start = new Point(start);
this.end = new Point(end);
}

// Create a line from two coordinate pairs
public Line(double xStart, double yStart, double xEnd, double yEnd) {
start = new Point(xStart, yStart); // Create the start point
end = new Point(xEnd, yEnd); // Create the end point
}

// Calculate the length of a line
public double length() {
return start.distance(end); // Use the method from the Point class
}

// Convert a line to a string
public String toString() {
return "(" + start+ "):(" + end + ")"; // As "(start):(end)"
} // that is, "(x1, y1):(x2, y2)"

// Return a point as the intersection of two lines -- called from a Line object
public Point intersects(final Line line1) {
Point localPoint = new Point(0, 0);

double num =
(this.end.y - this.start.y)*(this.start.x - line1.start.x) -
(this.end.x - this.start.x)*(this.start.y - line1.start.y);

double denom =
(this.end.y - this.start.y)*(line1.end.x - line1.start.x) -
(this.end.x - this.start.x)*(line1.end.y - line1.start.y);

localPoint.x = line1.start.x + (line1.end.x - line1.start.x)*num/denom;
localPoint.y = line1.start.y + (line1.end.y - line1.start.y)*num/denom;

return localPoint;
}
}
---------------------------------------------------------------------
SOURCE of Point.java
---------------------------------------------------------------------
package com.ars.geometry;

public class Point {
// Coordinates of the point
public double x;
public double y;

// Create a point from coordinates
public Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}

// Create a point from another Point object
public Point(final Point oldPoint) {
x = oldPoint.x; // Copy x coordinate
y = oldPoint.y; // Copy y coordinate
}

// Move a point
public void move(double xDelta, double yDelta) {
// Parameter values are increments to the current coordinates
x += xDelta;
y += yDelta;
}

// Calculate the distance to another point
public double distance(final Point aPoint) {
return Math.sqrt(
(x - aPoint.x)*(x - aPoint.x) + (y - aPoint.y)*(y - aPoint.y) );
}

// Convert a point to a string
public String toString() {
return Double.toString(x) + ", " + y; // As "x, y"
}
}
----------------------------------------------------------
SOURCE of TryGeometry.java
----------------------------------------------------------
package packageex2;

import com.ars.geometry.*;

public class TryGeometry {
public static void main(String[] args) {
// Create two points and display them
Point start = new Point(0.0, 1.0);
Point end = new Point(5.0, 6.0);
System.out.println("Points created are " + start + " and " + end);


// Create two lines and display them
Line line1 = new Line(start, end);
Line line2 = new Line(0.0, 3.0, 3.0, 0.0);
System.out.println("Lines created are " + line1 + " and " + line2);

// Display the intersection
System.out.println("Intersection is " + line2.intersects(line1));

// Now move the end point of line1 and show the new intersection
end.move(1.0, -5.0);
System.out.println("Intersection is " + line1.intersects(line2));
}
}
--------------------------------------------------------------
3. STAGE
Right click on PackageEx3 in the projects window. Select Properties. Select Libraries. Select Compile to add compile time libraries. You can add JAR or folder with this utility. We will add the source directories that we have created in the second stage, namely ../PackageEx2/src . Here it is critically important that you add one higher level of directory in order to include the dir you need. You must also add the same library to the run timr libraries by selecting the run tab.

---------------------------------------------------------------
SOURCE of TryGeometry.java
---------------------------------------------------------------
package packageex3;

import com.ars.geometry.Line;
import com.ars.geometry.Point;

public class TryGeometry {
public static void main(String[] args) {

// Create two points and display them
Point start = new Point(0.0, 1.0);
Point end = new Point(5.0, 6.0);
System.out.println("Points created are " + start + " and " + end);


// Create two lines and display them
Line line1 = new Line(start, end);
Line line2 = new Line(0.0, 3.0, 3.0, 0.0);
System.out.println("Lines created are " + line1 + " and " + line2);

// Display the intersection
System.out.println("Intersection is " + line2.intersects(line1));

// Now move the end point of line1 and show the new intersection
end.move(1.0, -5.0);
System.out.println("Intersection is " + line1.intersects(line2));
}
}
---------------------------------------------------------------
4. STAGE
We create a JAR file from the material that resides in the com.ars.geometry directory.

C:\Documents and Settings\Ali Riza SARAL\Desktop>jar
Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...
Options:
-c create new archive
-t list table of contents for archive
-x extract named (or all) files from archive
-u update existing archive
-v generate verbose output on standard output
-f specify archive file name
-m include manifest information from specified manifest file
-e specify application entry point for stand-alone application
bundled into an executable jar file
-0 store only; use no ZIP compression
-M do not create a manifest file for the entries
-i generate index information for the specified jar files
-C change to the specified directory and include the following file
If any file is a directory then it is processed recursively.


Example 1: to archive two class files into an archive called classes.jar:
jar cvf classes.jar Foo.class Bar.class
Example 2: use an existing manifest file 'mymanifest' and archive all the
files in the foo/ directory into 'classes.jar':
jar cvfm classes.jar mymanifest -C foo/ .



C:\Documents and Settings\Ali Riza SARAL\Desktop>pwd
/cygdrive/c/Documents and Settings/Ali Riza SARAL/Desktop

C:\Documents and Settings\Ali Riza SARAL\Desktop>cd geometry

C:\Documents and Settings\Ali Riza SARAL\Desktop\geometry>jar cvf geometry.jar -C . .


C:\Documents and Settings\Ali Riza SARAL\Desktop\geometry dizini
08/06/2009 09:52 PM DIR .
08/06/2009 09:52 PM DIR ..
08/06/2009 09:05 PM DIR com
08/06/2009 09:52 PM 3,569 geometry.jar

Adding the geometry jar for run time libraries option is NOT ENOUGH. You must also specify a package lib for the compile time libraries. I added the packageex2 libraries and saw that netbeans required the compile time libraries reference when using a package.

--------------------------------------------------
5. STAGE
I took out the compile time libraries reference to the packageex2 directory and referenced the geometry.jar for both the compile and run time libraries references. Be aware that this requires that your JAR file must have both the source and the class files of your package.

References:
1- Wrox - Beginning Java 2 JDK5 Edition -2005 by Ivor HORTON
2- Java Package Tutorial by Patrick Bouklee
3- Q&A How do I create a _JAR file – google search keywords.