0% found this document useful (0 votes)
7 views

Packages

The document discusses Java packages, creating packages, setting the CLASSPATH, creating JAR files, and importing packages. Packages are used for resolving naming conflicts, access control, and distributing reusable classes. The document provides examples of creating packages, setting the CLASSPATH, creating JAR files, and using import and static import statements.

Uploaded by

Harendra Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Packages

The document discusses Java packages, creating packages, setting the CLASSPATH, creating JAR files, and importing packages. Packages are used for resolving naming conflicts, access control, and distributing reusable classes. The document provides examples of creating packages, setting the CLASSPATH, creating JAR files, and using import and static import statements.

Uploaded by

Harendra Tomar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

➔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;

public class Circle


{
private double radius;
public Circle(double radius) { this.radius = radius; }
public double getRadius() { return radius; }
public void setRadius(double radius) { this.radius = radius; }
public String toString() { return "Circle[radius=" + radius + "]";
}
}
To compile the source using JDK, we need to use the -d option to specify the base directory
of the compiled class, i.e., "classes", as follows::
> javac -d classes src/com/yyy/Circle.java

• The generated class will be stored as "classes\com\yyy\Circle.class".


• Sub-directories "com" and "yyy" were created automatically with the -d option.

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;

public class TestCircle


{
public static void main(String[] args)
{
Circle c1 = new Circle(1.23);
System.out.println(c1);
}
}
We need to use the -cp (or -classpath) option to specify the base directory of the package com.yyy,
in order to locate com.yyy.Circle.class.

d:\myTest> javac -cp d:\myProject\classes TestCircle.java

To run the TestCircle,

d:\myTest> java -cp .;d:\myProject\classes TestCircle

➔ CLASSPATH Setting for Packages

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>

The above error is resolved when CLASSPATH is set.

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.

➔Making JAR Files for Library Packages:


A JAR (Java Archive) is a package file format typically used to aggregate many Java class files
and associated metadata and resources (text, images, etc.) into one file to distribute application
software or libraries on the Java platform.
1. Create a JAR file
In order to create a .jar file, we can use jar cf command in the following ways as discussed
below:
Syntax:
jar cf jarfilename inputfiles

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:

C:\> jar cf pack.jar pack

2. View a JAR file


Syntax:
jar tf jarfilename

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:

C:/> jar tf pack.jar

Now, the contents of pack.jar are displayed as follows:

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:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

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:

java -jar MyJar.jar

➔Import and Static Import Naming Convention for Packages

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));
}
}

Static Import Statement


• Static imports will import all static data so that can use without a class name.
• A static import declaration has two forms, one that imports a particular static
member which is known as single static import and one that imports all static
members of a class which is known as a static import on demand.
• Static imports introduced in Java5 version.
• One of the advantages of using static imports is reducing keystrokes and re-
usability.
Example:
import static java.lang.Math.*;
class Test2 {
public static void main(String[] args)
{
System.out.println(sqrt(4));
System.out.println(pow(2, 2));
System.out.println(abs(6.3));
}
}

You might also like