A Java application will typically consist of many classes, sometimes hundreds or even thousands of classes.
Becasue Java requires each public class to be part of separate file, you end up with atleast as many files as classes.
This can easily become unmanageable when it comes to working with your classes, finding files, or installing and distributing your application.
Sun has defined a standard packaging mechanism for placing related classes into packages.
Creating a Package:
In a large application or library, Java classes are usually organized into packages.
To put a class into a package, you simply include a package statement, such as
package com.timothyfisher.book;
at the beginning of class file.
The package statement must be the first non-comment line of class file.
The package name of a class becomes part of its full name. Example, if we had a class named MathBook in the com.timothyfisher.book package, the fully specified class name would be com.timothyfisher.book.MathBook.
Package names also dictate the directory structure in which the class source files are stored.
Each element in the patch name represents a directory.
Example, if your source code root directory is /project/src, the source code for MathBook class would be stored in the following directory path:
project/src/com/timothyfisher/book/
When executing a Java application from the command line using the java executable, you must include the full package name when specifying the main executable class.
For the MathBook example,
java com.timothyfisher.book.MathBook
This command would be executed from the root of the package structure. In this case, the directory above the com directory. In this case, it is /project/src/
While compiling the program to craete class files in a another directory and to create the package hierarchy use the following command:
Example: you have /project/classes directory to hold the class files.
Currently you are at /project/src direcotry and running the following command:
javac -d ..\classes com\timothyfisher\book\MathBook.java
-d option tells to create the hierarchy structure from the specified direcotry.
This will create hierarchy structure as: /project/classes/com/timothyfisher/book and it will have MathBook.class file.
To execute the file, run the execution from /project/classes directory
java com.timothyfisher.book.MathBook
or to run from any other directory:
java -cp /project/classes com.timothyfisher.book.MathBook
Archiving Classes with JAR
The jar utility is included with the JDK and is used to package groups of classes into a bundle.
Using the jar tool, you can create, update, extract,list and index a JAR file.
For the above example:
To create a jar file, run the following command at /project/classes directory.
jar cf project.jar .
Create a jar file to only include class files :
jar cf project.jar *.class
The c option tells jar utility to create a new jar file.
The f option is always followed by a file-name specifying the name of the JAR fiel to use.
Applications can also be extracted out of a JAR file.
All classes contained in a JAR file can be easily included on the CLASSPATH when running or compiling a Java application or library.
To include the contents of a JAR file in the CLASSPATH, you simply include the path to the JAR file instead of a directory.
CLASSPATH=.;/projects/classes/project.jar;
Running a program from JAR file:
Running a program from JAR without having a manifest class or main class specified, use the following format:
java -cp classes\project.jar com.raghu.book.MathBook
Using the java command-line executable, you can execute a Java applicaiton that is packaged in a JAR file. This is with manifest file.
But for this to happen, you must specify the class containing the main() method in the manifest file.
For example, for our example, please create a manifest file with these entries:
Manifest-Version: 1.0
Created-By: 1.7.0_51 (Oracle Corporation)
Main-Class: com.raghu.book.MathBook
Save the manifest file with name: MANIFEST.MF.
Now create the jar file to include the manifest file as well.
jar cfm project.jar manifest.mf .
This will create the jar and includes the manifest file as well.
Now to run the example:
java -jar project.jar
This will execute the main() from the specified executable class mentioned in manifest file.
Becasue Java requires each public class to be part of separate file, you end up with atleast as many files as classes.
This can easily become unmanageable when it comes to working with your classes, finding files, or installing and distributing your application.
Sun has defined a standard packaging mechanism for placing related classes into packages.
Creating a Package:
In a large application or library, Java classes are usually organized into packages.
To put a class into a package, you simply include a package statement, such as
package com.timothyfisher.book;
at the beginning of class file.
The package statement must be the first non-comment line of class file.
The package name of a class becomes part of its full name. Example, if we had a class named MathBook in the com.timothyfisher.book package, the fully specified class name would be com.timothyfisher.book.MathBook.
Package names also dictate the directory structure in which the class source files are stored.
Each element in the patch name represents a directory.
Example, if your source code root directory is /project/src, the source code for MathBook class would be stored in the following directory path:
project/src/com/timothyfisher/book/
When executing a Java application from the command line using the java executable, you must include the full package name when specifying the main executable class.
For the MathBook example,
java com.timothyfisher.book.MathBook
This command would be executed from the root of the package structure. In this case, the directory above the com directory. In this case, it is /project/src/
While compiling the program to craete class files in a another directory and to create the package hierarchy use the following command:
Example: you have /project/classes directory to hold the class files.
Currently you are at /project/src direcotry and running the following command:
javac -d ..\classes com\timothyfisher\book\MathBook.java
-d option tells to create the hierarchy structure from the specified direcotry.
This will create hierarchy structure as: /project/classes/com/timothyfisher/book and it will have MathBook.class file.
To execute the file, run the execution from /project/classes directory
java com.timothyfisher.book.MathBook
or to run from any other directory:
java -cp /project/classes com.timothyfisher.book.MathBook
Archiving Classes with JAR
The jar utility is included with the JDK and is used to package groups of classes into a bundle.
Using the jar tool, you can create, update, extract,list and index a JAR file.
For the above example:
To create a jar file, run the following command at /project/classes directory.
jar cf project.jar .
Create a jar file to only include class files :
jar cf project.jar *.class
The c option tells jar utility to create a new jar file.
The f option is always followed by a file-name specifying the name of the JAR fiel to use.
Applications can also be extracted out of a JAR file.
All classes contained in a JAR file can be easily included on the CLASSPATH when running or compiling a Java application or library.
To include the contents of a JAR file in the CLASSPATH, you simply include the path to the JAR file instead of a directory.
CLASSPATH=.;/projects/classes/project.jar;
Running a program from JAR file:
Running a program from JAR without having a manifest class or main class specified, use the following format:
java -cp classes\project.jar com.raghu.book.MathBook
Using the java command-line executable, you can execute a Java applicaiton that is packaged in a JAR file. This is with manifest file.
But for this to happen, you must specify the class containing the main() method in the manifest file.
For example, for our example, please create a manifest file with these entries:
Manifest-Version: 1.0
Created-By: 1.7.0_51 (Oracle Corporation)
Main-Class: com.raghu.book.MathBook
Save the manifest file with name: MANIFEST.MF.
Now create the jar file to include the manifest file as well.
jar cfm project.jar manifest.mf .
This will create the jar and includes the manifest file as well.
Now to run the example:
java -jar project.jar
This will execute the main() from the specified executable class mentioned in manifest file.
No comments:
Post a Comment