The
java command expects a
fully-qualified class name and not a filename. (Unlike the compiler,
javac, which expects a filename of a source file).
To run ClassB, use the command:
java packageb.ClassB
(with a dot, not a slash).
There are a few things to remember with regard to packages:
1. The directory structure has to match the packages of your source files. It looks like you have that correct.
2. When you run a program with packages, then make sure you have the base directory of the package hierarchy in the classpath. If you don't specify the classpath explicitly (through the
-cp or
-classpath option or the
CLASSPATH environment variable), then Java will take the current directory as the classpath, so then
you should run your program from the base directory of the package hierarchy. In your example, you should be in the directory
C:\temp on the command line.
So, this should work:
C:\temp> javac packagea\ClassA.java
C:\temp> javac packageb\ClassB.java
C:\temp> java packageb.ClassB