Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath question from K&B

 
raghu dubey
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

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

Yours,
Bu.
 
Doug Slattery
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic