Hi Lin Ahmad,
First of all, a warm welcome to CodeRanch!
Lin Ahmad wrote:Ques # 1: It seems that the Goo.java file in the book package is unable to see the other package, cert. As such the javac command should not be run in the book folder, am I right?
Lin Ahmad wrote:Ques # 2: The compilation worked because at the level of the TestOtherJavaFiles folder, both the cert and the book folders are visible?
When you execute the
javac (and
java) command, the current directory is used as the
class path (if you don't use the
-cp or
-classpath option). The class path (as you probably know) is the location where the
javac (and
java) command has to look for other classes. The
Sludge class doesn't have any dependencies on other classes, so you don't need additional classes added to the class path and you can compile this class from both the
TestOtherJavaFiles or
cert directory. The
javac command will be slightly different. From the
TestOtherJavaFiles directory you use this
javac command
And from the
cert directory this
javac command will compile the
Sludge class
Both will successfully compile the
Sludge class and create a
Sludge.class file in the
cert directory.
It's a different story with the
Goo class, because this class depends on the
Sludge class. So you have to make sure that the dependent classes (in this case the
Sludge class) are on the class path when compiling the
Goo class (otherwise you'll get an error when compiling as you have experienced). And which directory contains both classes? It's the
TestOtherJavaFiles directory. So if you execute the
javac command from this directory, the
TestOtherJavaFiles directory will be used as current directory and the
Sludge class will be found and the
Goo class is compiled successfully. And the
javac command is very similar to the one to compile the
Sludge class from the
TestOtherJavaFiles directory
And as with the
Sludge class, you can also use the
javac command from the
book directory to compile the
Goo class. But it's a bit harder as you need to set the class path explicitly (to include the
Sludge class). Because if you execute the
javac command from the
book directory, the
book directory is by default the current directory and the
Sludge class won't be found (resulting in an error during compilation as you have experienced). So the
javac command from the
book directory to compile the
Goo class looks like
So the classpath in this command consists of two directories (on a Windows machine you must use the semicolon as a separator, on a Unix/Linux machine you must use the colon). The first one is
. (the dot character) which represents the current directory. If you define a class path (using the
-cp or
-classpath option), you must explicitly add the current directory (if required). The second one is the relative path
.. which denotes the parent directory of the
book directory (which is the
TestOtherJavaFiles directory in this example). Another alternative was to use an absolute path (instead of the relative path)
Because the current directory is not actually required to compile the
Goo class from the
book directory, it can be removed from the
javac command
All these
javac commands will successfully compile the
Goo class and create a
Goo.class file in the
book directory.
Lin Ahmad wrote:Ques # 3: I tried to execute Goo.class (this has the main method) in the three directories below, none worked. It could not find the main class. Why?
That's a much easier explanation. Phew!
When you execute the
java command you have again to make sure that all required classes are on the class path. So for this example it's the easiest to execute the
java command from the
TestOtherJavaFiles directory as this directory contains both classes and it will be the current directory. When executing the
java command you must use the fully qualified name of the class containing the
main method. So in this example, that's
book.Goo (as you can see you must not use the
.class suffix). So if we combine both suggestions, you can execute the following
java command from the
TestOtherJavaFiles directory to successfully execute the application
(which will print
sludge ). But as with the
javac command, you can execute the
java command from another directory as well, but you need to add the appropriate directory (in this case the
TestOtherJavaFiles directory) to the class path. So if you are in the
book directory, you'll need to use this
java command
As you can see that's again very similar to the
javac command to compile the
Goo class from the
book directory. Do you want to run the application from the
cert directory? The
java command will be exactly the same
Do you want to execute the application from a random directory (e.g.
C:\Windows\System32)? Use the absolute path to the
TestOtherJavaFiles directory as the class path
Hope it helps!
Kind regards,
Roel
PS. I was impressed with the quality of your post: a nice overview of all steps you have executed and issues you have encountered, tried to look for an answer to solve your issues, asked clear questions about your doubts/issues and you
UseCodeTags. Have a
cow for this excellent post!