wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem in classpath and jar files and searching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem in classpath and jar files and searching" Watch "problem in classpath and jar files and searching" New topic
Author

problem in classpath and jar files and searching

akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
hi ,
i just finished chapter 10 of the kathy and bates book . I am really confused on these topics :
-declaring and usingclasspath , jar files and searching .

please point me to additional resources on these topics .

Specifically what is the use of the "." in the classpath in the java command . It is used to search the current directly for the files may be used in the file being executed being executed or would we need it for locating the file being executed also.. i mean in the following

java -classpath . FindBaz // FindBaz.class being present in directory "X" and the current directory being X

so the"." is needed for locating files needed by FindBaz or is it needed to locate the FindBaz itself ? This is in reference to Question no 9./self test/chapter10/kathy and bates/scjp 6.

And is it right that we dont need "." in case of javac ,as it automatically searches the current directory ?

Regards
Akash
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

The best resource would be to practice it yourself by making some jars. The . in the classpath refers to the current directory. The classpath is needed to load any classes that are not found in the default java packages.

[EDIT]

This link might help

http://www.ibm.com/developerworks/library/j-classpath-windows/

A windows specific article. The ant and maven references in the end can be ignored.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
So is the "." present in the default classpath also ? Because we never need to use the classpath when executing a file from the directory in which it is present.

Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Hey Akaash,

That . tells the jvm to look for the files(jars) used by FindBaz.class in the current directory which would be same as where FindBaz.class is. You are running this command from where this class is.

Regards


Experience and talent are independent of age
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
but do we need the "." to locate the "FindBaz.class also ?
nd what about javac. Is "." required dere ?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

look in java -classpath we need to find both the classes that is Findbaz and Baz class. So . looks in the current directory.It will find Findbaz but no baz class.You need both the files for execution so need to find both the files.

java -classpath test:. Findbaz will run Findbaz class in x and then will look for baz class in test and . i.e x
here also it will look first in test and then x.

then
java -classpath test:test/myApp:. Findbaz will run Findbaz class in x and then will look for baz class in test, test/myApp and x. The first match is taken and used with the Findbaz.

if you use java -classpath test Findbaz then it wont execute as the jvm cannot find Findbaz class in test.

in javac specifying classpath is to tell the compiler that there is a .java file that i like to compile and please find references that are used in the java file in files specified in classpath

in java specifying classpath tells the jvm that the class which is supposed to run is there in this directory and if there are any other classes please look in the directories which are specified.

Specifying classpath in javac or java overrides the classpath specification in the environmental variables which is the default used alwayz.

I hope you understood

Cheers


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
ya i hope . please correct me if i am wrong when i say :

lets suppose we need to compile and run XYZ.java. nd the current directory is the one in which XYZ is present
- in "javac" you dont need "." in class path as you dont need to find the file being complied . we only need to find the files being used by the file XYZ.java

- in "java" you need the "." in classpath to locate the XYZ.class also.

- If we execute XYZ.class from the directory in which XYZ is located , and dont override the classpath , we are able to find it because "." is present in the default classpath . But if we override the classpath then we need to explicitly add the "." else XYZ.class wont be found.


Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

basically these classpath things come when we use self defined packages and jar files to find the references. If you have a simple xyz.java and it has to find a Utils class in that file. First we import the package and then let the default classpath to find the utils class and then the current directort in which your xyx.class is present.

1 more thing you can specify absolute directories also which will search from the root rather than the current directory thing (relative). Using absolute directories will nullify the current directory.It doesn't matter which directory you are in as far as the file path is correct from the root.

And yes Akash you got the concept correctly.
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
so were all the three statements correct ? please reply . i need to hear it , it'll be a boost to my dwindling confidence level
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

in javac if we use -classpath then you will be needing . to find out the classes in the current directory used in your java file.

Btw you got the concept correct.... So don't worry about it.
akaash singh
Ranch Hand

Joined: Jul 04, 2009
Posts: 45
ya dat last thing too. Was thinking that i had missed it . neways thanks a lot mate.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: problem in classpath and jar files and searching