The classpath (list of) directory is used by the compiler and the JVM to find all the class files it uses. Interestingly, the java compiler doesn't need the Bar.class file -- because it doesn't need it to compile the Bar.java file.
The javac command is the Java compiler. When you enter a command such as javac Bar.java, then you are compiling the source file Bar.java to a class file Bar.class. Make sure you don't confuse "compiling" and "running": javac, the Java compiler, compiles source code to class files, and java, the launcher, runs class files.
The classpath tells the compiler (javac) or the Java launcher (java) where it should look for class files - not Java source files or anything else.
Raju Champaklal wrote:so it means we should use -classpath in the java command to include the directory which contains the class file of Bar.java....as it wouldnt need any other class to compile now
No. The java command doesn't do anything with source files such as Bar.java and does not compile your code. It just runs Java programs, and you might use the -classpath switch to tell it where to look for the file Bar.class.