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

[CSC221-2024-02-06]Packages and the Java Module System

Uploaded by

pm173919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

[CSC221-2024-02-06]Packages and the Java Module System

Uploaded by

pm173919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Object Oriented Programming in Java

Class 06: Packages and the Java Module System

Innocent Okoloko, PhD

https://www.youtube.com/@innocentokoloko/
5/17/2024 1
Learning Objectives

▪ At the end of the course, the participants should be able to:


❖ Explain Java Packages
❖ Import Classes and Packages
❖ Create and Work with Packages
❖ Create and run a JAR File
❖ Use Javadoc to Document Classes
❖ Explain the Java Module System
❖ Explain and create a module-info.java File
❖ Set up Module Folders & Compile and create modular jar files

5/17/2024 2
Motivation

▪ Working with packages and the new java module system is required for professional Java
development
▪ It is therefore important for future Java developers to have a good understanding of the
concepts

5/17/2024 3
Contents
1. Introduction to Packages

2. Importing Classes and Packages

3. Creating and Working with Packages

4. Putting Classes in a JAR File

5. Using Javadoc to Document Classes

6. Using the Java Module System


7. The module-info.java File

8. Setting up Module Folders & Compiling

5/17/2024 4
Introduction to Packages
▪ A package is a group of classes that belong together java.util
▪ Think of it as a folder in a file directory javax.swing
java.awt
▪ Packages are divided into two categories: java.io
1. Built-in Packages (packages from the Java API) java.lang
2. User-defined Packages (create your own packages)
▪ Packages are used to avoid name conflicts, and to write a better maintainable code
▪ Packages enable you to keep your classes separate from classes in the Java API
▪ Packages allow you to reuse your classes in other applications
▪ Packages let you distribute your classes to other people
▪ Javadoc lets you add documentation comments to your classes
▪ The Java Module System, provides an improved way of working with packages

5/17/2024 5
Importing Classes and Packages
▪ To use a class or a package from the Java API library, you need to use the import keyword
▪ When you use import statements at the beginning of a Java source file
▪ You make classes from the packages mentioned in the import statements available
throughout the file
▪ You can import all the classes in a package by using an asterisk wildcard
▪ Syntax: import package.name.Class; // Import a single class
import package.name.*; // Import the whole package
▪ E.g., import java.util.*;
import java.util.ArrayList;

▪ Note: You don’t have to use an import statement to use a class from a package
▪ In that case, you must fully qualify any references to the class
▪ E.g., java.util.ArrayList<String> list = new java.util.ArrayList<String>()

5/17/2024 6
Importing Classes and Packages
▪ Two packages you never have to explicitly import:
1. java.lang: This package contains classes that are so commonly used that the Java
compiler makes them available to every program
➢ Examples of the classes in this package are String, Exception, and the various
wrapper classes, such as Integer and Boolean
2. The default package: This package contains classes that aren’t specifically put in some
other package
▪ All the programs we have seen so far in this course up to this point are in the default
package
▪ For simple program development and experimentation, using the default package is
acceptable
▪ However, if you start work on a serious Java application, create a separate package for it and
place all of the application’s classes there
5/17/2024 7
Creating and Working with Packages
▪ To create your own package, you need to understand that Java uses a file system directory to
store them
▪ Just like folders on your computer:

▪ To create a package, use the package keyword:


▪ Steps:
1. Pick a name for your package
➢ Try to follow the established convention of using your Internet domain name backwards
➢ E.g., com.inno
➢ Notice that package names are in all-lowercase letters (Java is case sensitive)
➢ You can add additional levels beyond the domain name if you want
➢ E.g., com.inno.arms
5/17/2024 8
Creating and Working with Packages
2. Choose a directory on your hard drive to be the root of your class library
➢ E.g., c:\javaclasses
3. Create subdirectories within the root directory for your package name
➢ For example, for the package named com.inno.arms, create a directory named com in
the c:\javaclasses directory
➢ Then, in the com directory, create a directory named inno
➢ Then, inside inno, create a directory named arms
➢ Thus, the complete path to the directory that contains the classes for the com.inno.arms
package is c:\javaclasses\com\inno\arms
4. Add the root directory for your package to the ClassPath environment variable
➢ Follow 01 Intro Programming with Python PT 1 - Introduction to Programming (youtube.com)
from 1:04:34

5/17/2024 9
Creating and Working with Packages
5. Save the files for any classes you want to be in a particular package in the directory for that
package
For example, save the files for a class that belongs to the com.inno.arms package in
C:...\javaclasses\com\inno\arms
6. Add a package statement to the beginning of each source file that belongs in a package

5/17/2024 10
Creating and Working with Packages
▪ Example
//packageTest0.java
package com.inno.arms;
import java.util.Scanner;
public class packageTest0{
static Scanner sc = new Scanner(System.in);
public static boolean askYorN(String prompt){
while (true){
String answer;
System.out.print("\n" + prompt + " (Y or N) ");
answer = sc.next();
if (answer.equalsIgnoreCase("Y"))
return true;
else if (answer.equalsIgnoreCase("N"))
return false;
}
}
}
5/17/2024 11
Creating and Working with Packages
▪ Compile the program so the .class file is stored in the C:...\javaclasses\com\inno\arms
directory also
▪ If you have added C:\...javaclasses to your ClassPath environment variable
▪ Then you can use test that your package is working:

5/17/2024 12
Creating and Working with Packages
▪ Create the test program, store it in the javaclasses directory
//packageTest1
//Stored in a different directory from \com\inno\arms
import com.inno.arms.*;
public class packageTest1{
public static void main(String[] args){
while (packageTest0.askYesOrNo("Keep going?")){
System.out.println("Ok!");
}
}
}

5/17/2024 13
Creating and Working with Packages
▪ How to compile

5/17/2024 14
Putting Classes in a JAR File
▪ A JAR (Java ARchive) file is a single executable file that can contain more than one class in a
compressed format
▪ JAR files contain a special file, called the manifest file, that contains information about the files
in the archive
▪ This manifest is automatically created by the jar utility, but you can supply a manifest of your
own to provide additional information about the archived files
▪ JAR files are a common way to distribute finished Java applications
▪ To create a JAR file, run the jar command
jar options jar-file [manifest-file] class-files
▪ The options specify the basic action you want jar to perform and provide additional
information about how you want the command to work
▪ See TABLE 8-1 in p371 of the main course textbook for the jar options

5/17/2024 15
Putting Classes in a JAR File
▪ Steps:
▪ 1. Open a command window
▪ 2. Use a cd command to navigate to your package root: cd \javaclasses
▪ 3. Use a jar command that specifies the options cf, the name of the jar file, and the path to the
class files you want to archive jar cf arms.jar *.class com\inno\arms\*.class
▪ 4. To verify that the jar file was created correctly, use the jar command
jar tf utils.jar
▪ Here's some typical output from this command:
META-INF/
META-INF/MANIFEST.MF
com/inno/arms/packageTest0.class

▪ Check if the manisfest file was created and try to run the jar file
java -jar arms.jar
5/17/2024 16
Putting Classes in a JAR File
▪ Adding the JAR to the classpath
▪ To use the classes in an archive, you should add the jar file to your ClassPath environment
variable, as follows:
▪ C:...\javaclasses\arms.jar;
▪ You can also add all the jar files from a particular directory to the ClassPath in one fell swoop,
as follows: C:...\javaclasses/*

5/17/2024 17
Using Javadoc to Document Classes
▪ The Javadoc tool can automatically create fancy HTML-based documentation based on
comments in your source files
▪ All you have to do is add a comment for each public class, field, and method; then run the
source files through the javadoc command
▪ Voilà! You have professional-looking, web-based documentation for your classes
▪ Adding Javadoc comments
▪ Javadoc comments must begin with /** and end with */
▪ You can place Javadoc comments in any of three different locations in a source file:
1. Immediately before the declaration of a public class
2. Immediately before the declaration of a public field
3. Immediately before the declaration of a public method or constructor
▪ A Javadoc comment can include text that describes the class, field, or method
▪ Each subsequent line of a multiline Javadoc comment usually begins with an asterisk
5/17/2024 18
Using Javadoc to Document Classes
▪ The text in a Javadoc comment can include HTML markup
▪ However, avoid using heading tags (<h1> and so on) because Javadoc creates those
▪ But you can use tags for boldface and italics (<b> and <i>) or to format code examples (use
the <pre> tag, which causes line endings and indentation to be preserved)
▪ In addition, you can include special doc tags that provide specific information used by Javadoc
to format the documentation pages

5/17/2024 19
Using Javadoc to Document Classes
▪ Table 8-2 p376

5/17/2024 20
Using Javadoc to Document Classes
▪ Example //Employee.java
package com.inno.payroll;
/** Represents an employee.
* @author Innocent Okoloko
* @version 1.7
* @since 1.0
* How to compile: javac com\inno\payroll\Employee.java;
* javadoc com\inno\payroll\*.java
*/
public class Employee{
private String lastName;
private String firstName;
private Double salary;
/** Represents the employee's address.*/
public Address address;
/** Creates an employee with the specified name.
* @param lastName The employee's last name.
* @param firstName The employee's first name.
*/
public Employee(String lastName, String firstName){
this.lastName = lastName;
this.firstName = firstName;
this.address = new Address();
}/** Gets the employee's last name.
5/17/2024 21
Using Javadoc to Document Classes
//Employee.java (continued)
* @return A string representing the employee's last name.
*/
public String getLastName(){
return this.lastName;
}
/** Sets the employee's last name.
* @param lastName A String containing the employee's
* last name.
*/
public void setLastName(String lastName){
this.lastName = lastName;
}
/** Gets the employee's first name.
* @return A string representing the employee's first
* name.
*/
public String getFirstName(){
return this.firstName;
}
/** Sets the employee's first name.
* @param firstName A String containing the
* employee's first name.

5/17/2024 22
Using Javadoc to Document Classes
//Employee.java (continued)
* employee's first name.
*/
public void setFirstName(String firstName){
this.firstName = firstName;
}/** Gets the employee's salary.
* @return A double representing the employee's salary.
*/
public double getSalary(){
return this.salary;
}
/** Sets the employee's salary.
* @param salary A double containing the employee’s salary.
*/
public void setSalary(double salary){
this.salary = salary;
}
}
class Address implements Cloneable{
public String street;
public String city;
public String state;
public String zipCode;
}
5/17/2024 23
Using Javadoc to Document Classes

5/17/2024 24
Using the Java Module System
▪ Java packages have some issues
▪ For example, developers often have problems managing packages for large applications that
use a large numbers of packages
▪ especially when those packages require different versions of Java
▪ Another problem with packages is that they don’t provide an easy way to create a lightweight
application
▪ whose runtime contains only those portions of Java that are actually needed
▪ This can limit Java’s ability to run on devices with limited resources, e.g., embedded controllers
or smartphones

5/17/2024 25
Using the Java Module System
▪ A module is a new way of grouping classes in a way that explicitly lists which other modules
the module depends on
▪ and what specific public types (classes/interfaces) within the module are to be made available
for other modules to use
▪ Specifically:
▪ A module must explicitly list its dependencies — i.e, what other modules are required for the
module to compile and run
▪ E.g., if one or more of the classes in the module require database access, the module must
explicitly indicate that it requires the Java database module (java.sql)
▪ A module must also explicitly list the visibility of any packages contained within it
▪ Recall that, you can create public types (classes) within a traditional package, which becomes
available throughout the package and also externally to the package

5/17/2024 26
Using the Java Module System
▪ With modules, public types in a package are visible outside of the module iff the package is
explicitly listed as an exported type
▪ A module is a JAR file
▪ A JAR file that contains a module is called a modular JAR file
▪ The only difference between a modular JAR file and a regular JAR file is that a modular JAR file
contains a special class called module-info.class (from module-info.java)
▪ The module-info.class class file identifies the module’s dependencies and the packages that
are visible to other modules

5/17/2024 27
The module-info.java File
//Simple module-info.java file that creates a module with no dependencies or exported
packages:
module com.inno.payments{}

//Not so simple module-info.java file


module com.inno.payments{
//add a requires statement to specify that the module is dependent on another module
requires java.sql;
requires com.inno.arms;
//add exports statements to export packages that are contained in the module
exports com.inno.payrolldb;
//Any public classes or interfaces defined by com.inno.payrolldb are visible to other
modules
}

5/17/2024 28
Setting up Module Folders & Compiling
▪ The module-info.java file must be in the root folder for the module
▪ The manifest file, if needed, can also be in the root folder for the module

Module

Package

▪ To compile
javac module-info.java com\inno\payments\*.java

5/17/2024 29
Setting up Folders for a Module
▪ Creating a modular JAR file
jar cf com.inno.payments.jar *.class com\inno\payments\*.class
▪ Running a modular JAR file

java -jar com.inno.payments.jar

5/17/2024 30
Recommended Textbooks
Doug Lowe, Java® All in One, 11th ed. John Wiley & Sons,
Hoboken, NJ, 2023
ISBN 978-1-119-98664-5,
https://www.amazon.com/dp/1119986648/

Kathy Sierra, Bert Bates, and Trisha Gee, Head First Java ,
3rd ed. O'Reilly Media, Sebastopol, CA, 2022
ISBN 978-149-191077-1,
https://www.amazon.com/dp/1491910771/

5/17/2024 31
Please, like, share, comment, and subscribe for more videos
Thank you and see you in the next class!

https://www.youtube.com/@innocentokoloko/
11/10/2022 32

You might also like