Packages in Java Tutorial Quick summary Packages enable
Packages in Java Tutorial
Quick summary • Packages enable one to group together related files. • Packages enable two classes to share the same name, but be unique by package. • Think directories • Fully qualified name includes package name: – java. lang. String – java. util. Array. List
For a package to be found • It’s absolute path must be in the classpath for that machine • A classpath is an environment variable that specifies the list of possible places that the OS should search for a given class (or package/class)
To make a package • Put the related classes into a directory which will be the name of the package. • You can include subdirectories: java. util says the java directory/util subdirectory. • The first line of each file should be the name of the package: package. Name. • Recompile the files
Naming a package • By convention and to avoid conflicts, package names always start with a small letter: • java. util • javax. swing
Do you always have to import a package? • You can import the whole package: import package. *; • You can import one class from the package: import package. class; • You can use the qualified name: package. class. method();
Jar files Tutorial
Jar Files • Enable you to package an application for deployment. • Includes the class files and optionally a manifest. • The manifest contains among other things the entry point to the application (the main).
In JGrasp • You can automatically build a jar file using the Project utility and then you can build the jar file. • It automatically builds a manifest with the name of the application.
To run an application • java –jar jarfilename
- Slides: 10