aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in ch-10(Kathhy Sierra) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in ch-10(Kathhy Sierra)" Watch "Doubt in ch-10(Kathhy Sierra)" New topic
Author

Doubt in ch-10(Kathhy Sierra)

Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 293
I am not clear with some topics in ch-10(Development).I have some doubts regarding "Searching for other classes","Declare and using classpaths","pkgs and searching" and finally "Relative and absolute paths". In all these things iam confusd with how java & javac command search for classes.

Book mentions few things like "java and javac commands DONOT look for .class files in currrent Directory i.e we need to add . in classpath." I have some confusion regarding these things as i personally feel no enough examples are provided.

Eg
suppose i have a file say "Nir.java" in pkg com\smart.
The parent directory of the pkg(com...) is (Say source)

C:\source>

and say classes are stored in folder classes...


c:/source/and the pkg..

I need to know how java and javac command will search for classes....
If someone can explain me or give some material to understand this topic, then it will be a great help...
[ December 20, 2007: Message edited by: JavaMan Shenoy ]

Man Suraj
Someone says "Impossible is nothing". The man next him says "Let me see you licking your elbow tip!"
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
For compiling this file, if the Nir class doesn't import any additional classes, you just have to say:

if you're not in the "smart" directory, or:

So for compiling the .java file you have to specify it's fully qualified name.

But when you try to run it with java, you have to specify a classpath and a fully qualified class name, no matter what's the current directory:

As you can see, java adds the classpath to the package tree of the class and tries to find the file in the resulting directory, like this:

Same goes when you try to compile Nir.java if it imports some additional classes. You have to specify the classpath to javac in the same way.
[ December 20, 2007: Message edited by: Serge Petunin ]
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 293
ok fine, but what does it mean when they say that "java and javac command dont search the current directory by default.You must tell them to search there ..and therefore add . to classpath."

what do they mean when they say that current directory is not searched by java and javac command(This is the case of .class files iam talking about.)
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
This means that this command line won't do:

This also won't do:

Without the classpath, java is blind It won't just add the package path to the current directory, and it won't search for the class file in the current directory, if you don't explicitly specify the "." in the classpath.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 293
Thanks Serge.i have got a better picture now.One more thing iamnot able to understand one questn of Kathysierra.related to directory structure(Ch-10)

Default classpath
/foo

foo has a subdirectory test which has a subdirectory xcom.

xcom contains two files, one A.class and B.java.



And two files

package xcom;
public class A
{
}

package xcom;
public class B extends A
{
}

Which allow B.java to compile´┐Ż.
Options given are

A.Set current dir to xcom and then invoke

Javac B.java

B. Set current dir to xcom and then invoke
Javac -classpath . B.java

C. Set current dir to test and then invoke
Javac -classpath . xcom/B.java

D. Set current dir to test and then invoke
Javac -classpath xcom:. B.java

Correct answer they are saying is c...

why option B is not correct?(though you are setting classpath to . , so i think you being in xcom dir, you sould be able to use classes in xcom...)

Also what does option D means?i.e does option D makes any sense?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

"JavaMan Shenoy", please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
you are setting classpath to . , so i think you being in xcom dir, you sould be able to use classes in xcom...

That's right, you're able to use CLASSES, not SOURCE FILES. A file B.java is a source file, classpath has nothing to do with it. For the javac compiler to find the source file, we have to either set the current directory to xcom (where B.java resides) or specify the full qualified name foo/test/xcom/B.java.

In D option we can see the standard way of specifying several directories in the classpath in Linux, using a colon ":". In Windows you use the semicolon ";" symbol. Though this option is incorrect anyway because we specify only the name of the file B.java, while in test directory. The compiler doesn't see the file, as B.java is in the xcom directory.
Maan Suraj
Ranch Hand

Joined: Dec 20, 2007
Posts: 293
Originally posted by Serge Petunin:

For the javac compiler to find the source file, we have to either set the current directory to xcom (where B.java resides) or specify the full qualified name foo/test/xcom/B.java.



Then why is option A wrong?Here i am setting current directory to xcom and then only i am invoking "javac B.java" .Here i believe javac compiler should be able to find the source file...but.....
Sergey Petunin
Ranch Hand

Joined: Dec 16, 2007
Posts: 44
Because in the B.java file the B class extends the A class from already compiled file - A.class. To find the CLASS file, like I said, the classpath for the javac is needed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in ch-10(Kathhy Sierra)
 
Similar Threads
Compiling using a JAR file in the classpath
how to compile a code with package
SCJP 5 Study Guide: Self Test Q. 12 - Page 788
K & B Book Chapter 10 Question 12
classpath and 'current directory'