How to wrapping your own packages in java, JAVA Programming

How to Wrapping Your Own Packages in java?

Java does not limit you to using just the system supplied packages. You can write your own as well. You write packages just such as you write any other Java program. Make sure you follow these rules:

1. There must be no more than one public class per file.
2. All files within the package must be named where classname is the name of the single public class in the file.
3. At the extremely top of each file in the package, before any import statements, put the statement
package myPackage;

To use the package in other programs, compile the .java files as commonly and then move the resulting .class files within the appropriate subdirectory of one of the directories referenced in your CLASSPATH environment variable. For example if /home/elharo/classes is in your CLASSPATH and your package is known as package1, then you would form a directory known as package1 in /home/elharo/classes and then put all the .class files in the package in /home/elharo/classes/package1.

For example,


public class URLSplitter {

public static void main(String[] args) {

for (int i = 0; i < args.length; i++) {
try {
URL u = new URL(args[i]);
System.out.println("Protocol: " + u.getProtocol());
System.out.println("Host: " + u.getHost());
System.out.println("Port: " + u.getPort());
System.out.println("File: " + u.getFile());
System.out.println("Ref: " + u.getRef());
catch (MalformedURLException e) {
System.err.println(args[i] + " is not a valid URL");



$ javac -d /home/elharo/classes
The -d flag to the compiler tells it to form the essential directories such as elharo/net in the specified directory. In this instance, URLSplitter.class is placed in /home/elharo/classes/com/macfaq/net. You can use the usual shell syntax such as. For the current directory or ~ for your home directory.

Posted Date: 5/16/2013 2:25:50 AM | Location : United States

Related Discussions:- How to wrapping your own packages in java, Assignment Help, Ask Question on How to wrapping your own packages in java, Get Answer, Expert's Help, How to wrapping your own packages in java Discussions

Write discussion on How to wrapping your own packages in java
Your posts are moderated
Related Questions
What is heuristic? Heuristic is a procedure which commonly, but not always, works or that provides nearly the right answer.

Explain about Drawing Lines? Drawing straight lines along with Java is easy. Just call g.drawLine(x1, y1, x2, y2) where (x1, y1) and (x2, y2) are the endpoints of your lines

search a file with specific extension

I have to write a several line currency exchange rate wants me to use a variable and prompt var dollarAmount = Prompt("Enter amount in U.S. dollars:"); and give t

What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer required by a pr

Specifications 1. readNumberOfEntries → This function will ask the user for a value greater than or equal to two. The function will keep asking values as long as the user prov

I have three data structures projects that I need help with.

What is the difference between an if statement and a switch statement? The if statement is used to select between two alternatives. It uses a boolean expression to decide whi

Calculate the kilometer within an hour? class Car { String licensePlate = ""; // e.g. "New York 543 A23" double speed = 0.0; // in kilometers per hour doubl

Struts components can be classify into Model, View and Controller: ? Model: Components like business logic /business processes and data are the type of model. ? View: HTML, J