Packages
Packages
A package is a collection of related Java entities (such as classes, interfaces, exceptions, errors
and enums). Packages are used for:
1. Resolving naming conflict of classes by prefixing the class name with a package name.
2. Access Control: Besides public and private, Java has two access control modifiers
– protected and default – that are related to package.
3. For distributing a collection of reusable classes, usually in a format known as Java Archive
(JAR) file
➔Creating/Defining Packages
To make a class as part of a package, you have to include the package statement as the first
statement in the source file.
Example 1
We shall write a class called Circle in package com.yyy. It is a good practice to store the source
files and the classes in separate directories, typically called "src" and "classes". This is to facilitate
the distribution of classes without the source files.
Suppose that our base directory is d:\myProject.
Create two sub-directories "src" and "classes".
Write the Circle.java and save under "src\com\yyy", as follows:
package com.yyy;
Let's write a test program to use this Circle class. Suppose that TestCircle.java (in default package)
is saved in d:\myTest.
// d:\myTest\TestCircle.java
import com.yyy.Circle;
CLASSPATH describes the location where all the required files are available which are used in
the application. Java Compiler and JVM (Java Virtual Machine) use CLASSPATH to locate the
required files. If the CLASSPATH is not set, Java Compiler will not be able to find the required
files and hence will throw the following error.
Error: Could not find or load main class <class name>
1. Select Start
2. Go to the Control Panel
3. Select System and Security
4. Select Advanced System settings
5. Click on Environment Variables
6. Click on New under System Variables
7. Add CLASSPATH as variable name and path of files as a variable value.
8. Select OK.
Note: Semi-colon (;) is used as a separator and dot (.) is the default value of CLASSPATH in the
above command.
Here, cf represents to create the file. For example , assuming our package pack is available in
C:\directory , to convert it into a jar file into the pack.jar , we can give the command as:
Here, tf represents the table view of file contents. For example, to view the contents of our
pack.jar file, we can give the command:
META-INF/
META-INF/MANIFEST.MF
pack/
pack/class1.class
pack/class2.class
..
..
Here class1, class2, etc are the classes in the package pack. The first two entries represent that
there is a manifest file created and added to pack.jar. The third entry represents the sub-directory
with the name pack and the last two represent the files name in the directory pack.
3. Running a JAR file
• Keep in mind that not every JAR file is runnable. Some JAR files are just support files
intended to be used by other Java apps.
• For a JAR file to run, the JAR itself must contain at least one class that has a runnable main
method.
• Furthermore, the class with the main method must be listed as the main-class in the JAR’s
manifest file. If not, the class must be explicitly stated on the command line when the JAR
is run.
• If you have an application bundled in a JAR file, you need some way to indicate which
class within the JAR file is your application's entry point. You provide this information
with the Main-Class header in the manifest, which has the general form:
Main-Class: classname
The value classname is the name of the class that is your application's entry point.
An Example
We want to execute the main method in the class MyClass in the package MyPackage when we
run the JAR file.
We first create a text file named Manifest.txt with the following contents:
Main-Class: MyPackage.MyClass
Warning: The text file must end with a new line or carriage return. The last line will not be
parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:
This creates the JAR file with a manifest with the following contents:
Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: MyPackage.MyClass
When you run the JAR file with the following command, the main method of MyClass executes:
import statement
• To access a class or method from another package we need to use the fully qualified
name or we can use import statements.
• The class or method should also be accessible. Accessibility is based on the access
modifiers.
• Private members are accessible only within the same class. So we won't be able to
access a private member even with the fully qualified name or an import statement.
• The java.lang package is automatically imported into our code by Java.
Example:
class Geeks {
public static void main(String[] args)
{
System.out.println(Math.sqrt(4));
System.out.println(Math.pow(2, 2));
System.out.println(Math.abs(6.3));
}
}