Lecture 9.2 - User Defined Package
Lecture 9.2 - User Defined Package
Handling
Course Code: CSC 1205 Course Title: Object Oriented Programming 1
To create a package, choose a name for the package and put a package
statement with that name at the top of java source file.
The package statement must be the first line in the source file.
There can be only one package statement in each source file.
D:\demo\greeting
HelloWorld.java
Naming Convention
Package Naming Convention
As we know, packages are mirrored by directories, so how does the Java run-
time system know where to look for packages that we create.
The answer has three parts:
First, by default, the Java run-time system uses the current working
directory as its starting point.
Second, you can specify a directory path or paths by setting the
CLASSPATH environmental variable.
Third, you can use the -classpath option with java and javac to specify
the path to your classes.
Let’s take an example.
Finding Packages and
CLASSPATH
Example Finding Packages and CLASSPATH
Let’s take our previous code. And our HelloWorld.java file location is: D:\
MyProgram\Java\greeting
javac greeting/HelloWorld.java
It will create class file in the greeting directory, Next, we can run the file using
the following command.
java greeting/HelloWorld.java
Import Packages
How to import package
• https://docs.oracle.com/javase/tutorial/