Index


Overview


Simple steps to use Java packages:


Setting Up Packages


"packdemo" is our project root directory. Our packages start from this root dir.

The "model" and "test" directory are just simple computer directories.

However, they are also "packages" to Java
\---packdemo classpath dir
	|
	\----model package
	|
	\----test package
Add any Java files you want into the two directories.

Here we have one Java file into each package.
\---packdemo classpath dir
	|
	\----model Thing.java
	|
	\----test ThingTest.java
In each Java file we simply add a "package" statement as first statement in file so that identifies the file's owning package.

This file "Thing.java" is in the "model" package.
//Thing.java

package model;

public class Thing {

	public String whoAreYou() {
		return "I am Thing";
	}

}
In each Java file we simply add a "package" statement as first statement in file so that identifies the file's owning package.

This file "ThingTest.java" is in the "test" package. Here is the complete source....
ThingTest.java

package test;

public class ThingTest {
In each Java file we simply add a "package" statement as first statement in file so that identifies the file's owning package.

This file "ThingTest.java" is in the "test" package.
ThingTest.java

package test;

import model.Thing;

public class ThingTest {


Compiling and Running Using Packages


Note that many IDE's will do this work for you. However, even in that case, it is good to know how it actually works under the hood.

Using the above example let us assume this is our project dir:




This is a basic java compile.

The key is the classpath.

Classpath tells Java where to look for packages (in this example we give it the full path to "packdemo").
echo off
REM compile.bat

REM We can run this script from any dir
REM because classpath will tell Java where to look

set Root=C:\Work\Courses\packdemo

javac -classpath %Root% %Root%\model\Thing.java
javac -classpath %Root% %Root%\test\ThingTest.java

pause
This is a basic java run.

The key again is the classpath.

Classpath tells Java where to look for packages (in this example we give it the full path to "packdemo").
echo off

REM run.bat

REM We can run this script from any dir
REM because classpath will tell Java where to look

set Root=C:\Work\Courses\packdemo

java -classpath %Root% test.ThingTest

pause
If run these commands (or run this script) from the project dir "packdemo" then we can simplify classpath to "." as shown. The dot "." means current directory.
echo off

rem compileFromProjectDir.bat

REM Assuming we are in root dir (where packages live)
REM "." means current dir

javac -classpath "." model\Thing.java
javac -classpath "." test\ThingTest.java

pause
And running from the project dir (e.g. "packdemo") is similar to the previous compile step.
echo off

REM runFromProjectDir.bat

REM Assuming we are in root dir (where packages live)
REM "." means current dir

java -classpath "." test.ThingTest
pause


Title Here
Wow
Moo


Summary


Easy Does It
The kiss steps:

  • Files are organized into package dirs
  • Java files are associated with their owning package using a "package" statement line.
  • We use an "import" statement line to reference classes, etc in other packages.
\---packdemo classpath dir
	|
	\----model Thing.java
	|
	\----test ThingTest.java