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 ?
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.
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.
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
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Joined: Jul 04, 2009
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.
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.
Joined: Jul 04, 2009
so were all the three statements correct ? please reply . i need to hear it , it'll be a boost to my dwindling confidence level