And two files:

package xcom;
public class A{}

package xcom
public class B extends A{}

Given above directory structure where root dir is foo. Class B extends A.

Question is:
Which allows to compile?
The answer is: Set the current dir to test then invoke javac -cp . xcom/

My doubt is why the option below won't work?
1. Set the current dir to xcom then invoke javac -cp . ? Isn't this looking for A.class in xcom because of the (.) ?
2. By setting test as current dir, and using (.) isn't it searching for A.class only in test dir and not xcom?
3. Also it says in solution, because A.class is in xcom package, it will not find A.class if invoked from xcom dir. Why is that? Won't the (.) make it work?

if class A is in package xcom, javac will look for inside a folder (or jar) called xcom, starting from the classpath.
If the classpath includes the xcom directory javac will look in xcom for a folder (or jar) called xcom and, of course, it won't find one there.

Thanks James, I also wanted to ask although it might sound silly to you. Is there a difference at all between root dir and current dir?

And also it has to compile and execute And for that it needs A.class not would need to generate the classfile but it's already there). So why does it have to look for

Root dir (inthis context) would be a dir referenced in the class path - a dir where javac will start to look for package/class. Current dir is an OS dir that will be a root dir if . is in the classpath.

Yes, it's A.class. Sorry, that was me typing too fast! It looks for .class files in the classpath, not source files.

At this point I have to admit that I haven't compiled a multi-package app at the command line for about 15 years. Nowdays I'm 100% on Eclipse, where all these issues are handled differently. If you want to get any more into it, I'm probably not the best person to get involved.

I won't need to ask anyone else james thanks :) actually that typo between and A.class was confusing me. I have understood the topic :)

P.S. I would be very grateful if you could provide me some link on good and concise eclipse tutorial. Not something like Eclipse for dummies. I am little past that stage :)

Something like intermediate level tutorials link please?

It's so long since I started using Eclipse that I haven't looked at any current tutorials. So I can't suggest or recommend anythning; all I could do is Google - which I'm sure you can do just as well. Sorry. J

For every class defined in each source file compiled by javac, the compiler stores the resulting bytecodes in a class file with a name of the form classname.class. Unless you specify the -d option, the compiler places each class file in the same directory as the corresponding source file.

When the compiler must refer to your own classes you need to specify their location. Use the -classpath (-cp) option or CLASSPATH environment variable to do this. The class path is a sequence of directories (or zip files) which javac searches for classes not already defined in any of the files specified directly as command arguments. The compiler looks in the class path for both a source file and a class file, recompiling the source (and regenerating the class file) if it is newer.


The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath (-cp) command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

My doubt is why the option below won't work?
1. Set the current dir to xcom then invoke javac -cp . ? Isn't this looking for A.class in xcom because of the (.) ?

I tried javac -cp . and it did work for me