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 classname.java 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,
package com.macfaq.net;

import java.net.*;

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 URLSplitter.java
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 Resuscitation Trolley explain their features? A proper resuscitation kit is mandatory for the ICU. In addition to trolleys that can be moved around the ICU there should

Obtain ONE of the sample applets from Sun Microsystems' Applets page, or any other source for free Java applets you may wish to use. Test the applet, and link it to Part 2 of your

Project Description: Give a script that will produce a pop-under on a mobile landing page -We can host our own landing page and we will want to change the destination URL for

Define the difference between StringBuffer and String class?

It is a distinct type of loop, used to iterate through the properties of an object or the elements of an array. For instance assume the following statement that loops through th

using polymorphism to write a program to display digital clock

What is a local, member and a class variable? Variables declared within a method are "local" variables. Variables declared within the class i.e not within any methods are "m

The method should printout line numbers from 1 to howmany in the previous format -ONLY WHEN is divisble by 3 but is should not oupt more then 3 numbers on the line. input 1 outpu

Explain what is constructors ? It is frequent the case in which overloaded techniques are essentially the similar except that one supplies default values for a few of the argum

Implement your design in the Java programming language. Your main program file must be called MIPSim.java. Your simulator should implement the MIPS instruction set with the rest