foo
|-test
    |-xcom
        |-A.class
        |-B.java

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 B.java to compile?
The answer is: Set the current dir to test then invoke javac -cp . xcom/B.java

My doubt is why the option below won't work?
1. Set the current dir to xcom then invoke javac -cp . B.java ? 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 A.java 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 B.java. And for that it needs A.class not A.java(It would need A.java to generate the classfile but it's already there). So why does it have to look for A.java?

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 A.java 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 :)
Thanks.

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

Member Avatar for vishnu.khera.5

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.

http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/solaris/javac.html

--------------------------------------------------------------------------

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.

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/classpath.html

Member Avatar for vishnu.khera.5

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

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

Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.