• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in ch-10(Kathhy Sierra)

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"JavaMan Shenoy", please check your private messages. You can see them by clicking My Private Messages.
 
Sergey Petunin
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic