=====
CS 235 - Week 14 Lecture - 2021-11-29
=====
=====
TODAY WE WILL
=====
* announcements
* a few words on packages and JARs
* prep for next class
Reading:
* Core java course text
* Chapter 4 - Section 4.7 - Packages
* Chapter 4 - Section 4.8 - JAR files
* You should be working on Homework 9, due 11:59
Friday, Dec. 3rd
=====
packages and JARs, oh my!
=====
* starting with packages...
these provide a way to collect/organize related classes
together;
OR if you prefer:
Java allows you to group classes in a collection
called a package
* a rather common practice: to use reversed domain names
as package names...! (since Internet domains tend to be
unique...)
domain horstmann.com,
make a package com.horstmann...
domain humboldt.edu,
perhaps make a package edu.humboldt,
with packages such as
edu.humboldt.st10
edu.humboldt.dt5
etc.
For CS 235 related classes,
maybe I could put them in a package
edu.humboldt.st10.cs235
* fun fact: locating classes in packages is an activity
of the compiler -- Java bytecodes use FULL package names
for each class reference...!
* fun opinion: really, the C++ analogs to Java's package and
import are C++'s namespace and using
====
* ALL Java classes are considered as being in a package --
if one is not specified, that class is considered to be
in the default package... or the unnamed package...
(course text, p. 184)
=====
* BUT, say you don't want to use the unnamed package --
how do you DEFINE a package?
* STEP 1 - put a package statement at the TOP of your
source code file, BEFORE the code that defines the
classes in the package --
package <desiredPkgName>;
package edu.humboldt.st10.cs235;
* STEP 2 - PLACE this code in a subdirectory
that MATCHES the full package name
* now classes that import the class(es) in this
package can use them as we're used it,
IF the directory WITH the package subdirectories
is accessible from the CLASSPATH
CLASSPATH - the collection of all locations
that contain class files
...this can be an environment variable named CLASSPATH,
...this can also be specified using the -classpath
option
* [these worked in CS50 demo during class...]
javac -classpath /home/ubuntu/235pkg-play:. TryPkg.java
java -classpath /home/ubuntu/235pkg-play:. TryPkg
=====
how about JAR files?
=====
* JAR -- Java ARchive
a file format based on the ZIP file format
and it is used for aggregating many files
(including subdirectories) into one file
* NOT limited to .java and .class files!
frequently also includes image files,
audio files, subdirectories, etc.
* compressed!
* you can use the jar command to make JAR files
one way:
jar cvf desiredJarFileName.jar file1 file2 ...
* IF the JAR represents a Java application,
you can ask the jar tool to create a MANIFEST
file that includes which class has the main method,
and then you can have an executable JAR...
jar cvfe DesiredJarFileName.jar fullNameOfMainClass file1 file2 ...
* [these worked in CS50 demo during class...]
jar cvfe Try2.jar TryPkg *
jar cvfe DicePlay.jar TryJar *.class
* and (assuming your OS has a compatible version of the JVM
[Java Virtual Machine]
running?) you MIGHT be able to double-click on the
executable jar to execute its application!
* or -- again assuming your OS has a compatible version
of the JVM running -- you can run the executable jar
from the command line using:
java -jar DicePlay.jar
(this worked on CS50 IDE, in a Terminal open to a directory
that JUST contained DicePlay.jar)