There are three things to keep in mind when using packages:
1. Java expects package names to match the directory structure
2. It assumes the directory structure starts
from the working directory.
3. The fully qualified name of a class includes its package name.
Your first compile command
directory C:\java \p1.java
created the .class file in the c:\java
directory BUT the file included a package
statement so its fully qualified name became pack.p1
The JVM expects to find the .class file in a sub-directory named pack
When you compiled the file using
javac -d . p1.java
option directed the compiler to place the .class file in sub-directory pack
and that's the file structure you got: c:/java/pack/p1.class.
To run the file you don't need to change your classpath. Just make sure your working directory is one level above the package named directory. In your example, start at c:\java
and enter the command java pack.p1
. The compiler will look for the p1.class
file in the subdirectory pack
Hope that helps.