Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpath question from K&B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpath question from K&B" Watch "Classpath question from K&B" New topic
Author

Classpath question from K&B

raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Given two files:
package pkg;
public class Kit {
public String glueIt(String a, String b) { return a+b; }
}
import pkg.*;
class UseKit {
public static void main(String[] args) {
String s = new Kit().glueIt(args[1], args[2]);
System.out.println(s);
}
}
And the following sub-directory structure:
test
|--UseKit.class
|
com
|--KitJar.jar

If the current directory is test, and the file pkg/Kit.class is in KitJar.jar, which command line will produce the output bc ? (Choose all that apply.)

A. java UseKit b c
B. java UseKit a b c
C. java -classpath com UseKit b c
D. java -classpath com:. UseKit b c
E. java -classpath com/KitJar.jar UseKit b c
F. java -classpath com/KitJar.jar UseKit a b c
G. java -classpath com/KitJar.jar:. UseKit b c
H. java -classpath com/KitJar.jar:. UseKit a b c

The correct answer is definately either F or H. But the K&B says it is H. But why not F?.

The book says "B, D, and F are incorrect because java needs a classpath that specifies two directories, one
for the class file (the . directory), and one for the JAR file (the com directory). Remember,
to find a JAR file, the classpath must include the name of the JAR file, not just its directory"


QQ: Why do we need to have a .in the classpath. Java automatically finds the UseKit.class file as it is in the current directory.

Please help.

Thanks,
raghu.


Thanks,<br />Raghu.<br /> <br />SCJP 5, SCWCD 1.4, NCFM
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Hi Raghu,

Option F is not specifying the current directory. The current directory is not searched by default.

Aloha,
Doug

-- Nothing is impossible if I'mPossible
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60

yes but UseKit uses only the Kit class specified in com/KitJar.jar
so why we need to use :. in the classpath ?
the current directory doesn't hold any class we need to run our UseKit class.. or not?


SCJP5 - SCWCD5 - SCBCD5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy,

the current directory holds class Kit that is needed by the class in the jar file.

Yours,
Bu.


all events occur in real time
Doug Slattery
Ranch Hand

Joined: Sep 15, 2007
Posts: 294
Hi Al,
Originally posted by al nik:

yes but UseKit uses only the Kit class specified in com/KitJar.jar
so why we need to use :. in the classpath ?
the current directory doesn't hold any class we need to run our UseKit class.. or not?


True, but UseKit.class is *not* in com/KitJar.jar, but in the current directory. So the current directory we're in where UseKit.class is needs to be specified with the . .

Aloha,
Doug

-- Nothing is impossible if I'mPossible
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
OK. I seem to be able to understand this now. I created a program as below:


import static java.lang.System.out;

class UseKitTest{

public static void main(String... x){

out.println("Usekit in Package dom in the c directory");
}
}
==========
cd to the directory that contains the UseKitTest.class file:

#java UseKitTest..........this gave an error.
#java -cp . UseKitTest........ran fine.

So that means java commend is not even able to find a class file in current directory. In contrast javac can find the .java sourcefile correctly in the current directory without using classpath.

Thanks.
Raghu.
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
Howdy,

the current directory holds class Kit that is needed by the class in the jar file.

Yours,
Bu.

Kit is not needed by the class in the jar file.. Kit class is IN the jar file!
the current directory holds UseKit.class

If I have something like

with X in the current dir

javac X.java
and
java X

will compile and run correctly

I don't understand that if now I have UseKit.class in the current dir and I say to the compiler "take in the -cp this Jar that contains the Kit class that UseKit needs to run" this shouldn't work..
so
java -classpath com/KitJar.jar UseKit a b c
for me is correct but as you say it's not.. excuse me but I really don't understand why we need '.' in the -cp
thanks for your reply

p.s. I understand what you say in these posts if as we run the class X is considerated a 'special case'.. (maybe if we don't specify any -cp then java command should look in the current Dir by default and if we specify it java don't look at the current dir?)
[ October 25, 2007: Message edited by: al nik ]
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hey nik,

I guess you might have set '.' in your classpath somewhere in your environment variable.Thats why it works otherwise it'll never.Try removing your classpath env variable if defined any & execute the same 'X' class.It should flag an error.

Regards,
Priya.
raghu dubey
Ranch Hand

Joined: Jul 05, 2007
Posts: 72
Hi Al,

Can you please paste the below code in UseKitTest.java file. Put the file in C:\ drive. CMD to c drive and then run :

javac UseKitTest.java //this should work and compile the .class file
java UseKitTest //this should give error. If it runs then 100% you have the "." path set in the your CLASSPATH ot PATH env variable.

===========

import static java.lang.System.out;

class UseKitTest{

public static void main(String... x){

out.println("Usekit in Package dom in the c directory");
}
}

Thanks,
Raghu.
al nik
Ranch Hand

Joined: Oct 18, 2007
Posts: 60
Hey nik,

I guess you might have set '.' in your classpath somewhere in your environment variable.Thats why it works otherwise it'll never.Try removing your classpath env variable if defined any & execute the same 'X' class.It should flag an error.

Regards,
Priya.

Thanks a lot guys
I had '.' in my classpath environment variable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath question from K&B