aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class path query. 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 "Class path query." Watch "Class path query." New topic
Author

Class path query.

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

OCP Java SE 6 Programmer practice exam --> practice exam 3 --> Question No 56



What i understand is "-cp ." means finding the class files from current directory, and here current directory is $ROOT , so why not "-cp apollo/modules" should be right for compiling Saturn.java?
can anyone explain how this answer comes ?

Thanks in advance.


OCPJP 6 (91%)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

are you sure your directory structure is correct ? because Saturn.java is in package apollo, so it should be in apollo directory. but in your directory structure it is shown in $ROOT.

Also even if we assume that Saturn.java is in correct package viz in apollo then also i think none of the answers seem to correct.

consider option A The command for compiling is javac -d . -cp . Saturn.java . the classpath will be able to find Lunar.class but what about Module.class which is in controls.jar. how can A be correct ?

consider option C The command for compiling is javac -d . -cp .:controls.jar Saturn.java . it includes controls.jar in classpath but the actual command should be javac -d . -cp .:controls.jar apollo/Saturn.java


for running Saturn.class this option is correct java -cp .:controls.jar apollo.Saturn . it includes the current directory and path to controls.jar.

i don't have book for practice test exams so you please cross verify the question as well as the directory structure. Lets wait for other ranchers to post. meanwhile feel free to post .
Regards

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks gurpeet for your reply, that's very helpful.

I checked the question several time it stands as above.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Ankit Gareta wrote:Thanks gurpeet for your reply, that's very helpful.

I checked the question several time it stands as above.


then the best thing would be to create the directory structure given in the book, all the classes and try various options given in the options to see which works and not. that would be a good exercise in itself and will help you learn many more things.

And if you do the exercise , do post the result you got so that other ranchers get benefitted.
Regards
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 796
    
    1
Lunar's fully qualified name is apollo.modules.Lunar.
Module's fully qualifed name is apollo.modules.Module.
In -cp you only need to specify the root directory where apollo is. And apollo package is in $ROOT.
So, -cp . works.

If you put -cp apollo/modules, the compiler will search through apollo/module directory, look for apollo.modules.Lunar and apollo.module.Module classes. But it cannot find it.
If you have a directory like this: apollo/modules/apollo/module/Lunar.class and the Lunar's fully qualified name is the same apollo.modules.Lunar, then -cp apollo/modules will work.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

yeah ankit you are right. the question given in the book is same as you posted here. and i must thank you for providing good food for thought today. i created the complete structure on my system and analysed.

i was under the illusion that Saturn.java MUST be saved in package apollo and it would be error if not. i was wrong. generally we recommend that when you create a java file for e.g



it should be saved in guru folder as Test.java. i was under the illusion it MUST be saved in guru otherwise it would result in compilation error. but the fact is that it won't result in compilation error. but what will happen if i don't save it in guru folder ? it will compile and produce Test.class. the problem will be i won't be able to run it. here important thing to keep in mind is that javac Test.java will create Test.class but it WONT create guru folder.

in your case Saturn.java is in package apollo. so i thought it should be saved in apollo. but it wasn't. it was saved in root folder.

now coming back to your question .

to compile Saturn.java we need to specify classpath for Lunar.class(which is already compiled as per the question). Lunar.class has fully qualified name as apollo.modules.Lunar. now for javac to be able to find this class file(i.e. Lunar class file) we have to include PARENT OF THE PACKAGE ROOT in classpath . now parent of the package root apollo.modules is ROOT. so root should be in classpath which will enable javac to locate Lunar.class and hence compile Saturn.java. lets go over to options

A. javac -d . -cp . Saturn.java // here classpath is current directory ie root . that is what we want. root is the parent of the package root of apollo.modules

B. javac -d . -cp controls.jar Saturn.java // here classpath contains jar file . this will give error as to where is Lunar

C. javac -d . -cp .:controls.jar Saturn.java // here current directory is in classpath hence it will compile. it does not matter controls.jar is there or not. Saturn.java requires Lunar.class


i think you had problem with compiling. hope you understand the running part

hope you understood the explanation. and thanks for providing nice food for thought. please feel free to post if you have doubt


but i while analysing this i myself have one question. maybe you can help me .it is like this :

when i compiled Saturn.java from root folder say using option A i.e. javac -d . -cp . Saturn.java , what happens is that it creates Saturn.class and PUTS Saturn.class in apollo directory. however if you compile using javac -cp . Saturn.java , it creates Saturn.class right there in root directory. my question is that -d means where to put the generated class files. so "-d . " means in current directory, which is root SO "-d . " should result in Saturn.class in root directory BUT on the contrary it gets saved in apollo directory.

Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks for your reply Himai and gurpeet, that's very helpful.

"gurpeet wrote:
when i compiled Saturn.java from root folder say using option A i.e. javac -d . -cp . Saturn.java , what happens is that it creates Saturn.class and PUTS Saturn.class in apollo directory. however if you compile using javac -cp . Saturn.java , it creates Saturn.class right there in root directory. my question is that -d means where to put the generated class files. so "-d . " means in current directory, which is root SO "-d . " should result in Saturn.class in root directory BUT on the contrary it gets saved in apollo directory.


As I created the directory structure and run on my system, i comes with following thoughts.

if we compile the java file WITHOUT the option "-d" , it always put the class file in same directory where java file resides(doesn't matter what the package structure is).
if we compile the java file WITH the option "-d" , then it creates the package structure that describe in package statement in java file from -d option.
|--> exp, if "-d ." then it created the directory structure from root directory(for our example, it create apollo folder in root directory and put the class file into it)
|--> if "-d test/example", then it put the directory structure into test/example(should be exists within root directory) (for our example, it put class file in test/example/apollo).
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Hi All,

I have one query while running this program.

here, Lunar's fully qualified name is apollo.modules.Lunar,Module's fully qualifed name is apollo.modules.Module.
so after compiling the Satrun's file, its fully qualified name should be apollo.Satrun

so why running this Satrun file ( java -cp .:controls.jar apollo.Saturn ) apollo.Satrun is required
can't we direct say simply java -cp .:controls.jar Saturn ?

or we have to specify full path of file, from the root directory, which we want to compile ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Ankit Gareta wrote:Hi All,

I have one query while running this program.

here, Lunar's fully qualified name is apollo.modules.Lunar,Module's fully qualifed name is apollo.modules.Module.
so after compiling the Satrun's file, its fully qualified name should be apollo.Satrun

so why running this Satrun file ( java -cp .:controls.jar apollo.Saturn ) apollo.Satrun is required
can't we direct say simply java -cp .:controls.jar Saturn ?

or we have to specify full path of file, from the root directory, which we want to compile ?


yeah that is a nice observation regarding -d switch. i opened javac -help and checked that -d means WHERE TO PUT THE GENERATED CLASS FILES. but i think it actually does more than that. with -d switch specified it creates directory structure given in the package statement on the file system. without it, it DOESNOT.

Regarding your question the answer is NO.

java -cp .:controls.jar Saturn // this is wrong. you have to specify fully qualified classname for Saturn file while running.

java -cp .:controls.jar apollo.Saturn // this is correct.
marton nagy
Greenhorn

Joined: Jun 07, 2013
Posts: 5

Hi Everyone,



gurpeet singh wrote:

consider option A The command for compiling is javac -d . -cp . Saturn.java . the classpath will be able to find Lunar.class but what about Module.class which is in controls.jar. how can A be correct ?



My Question is the same as above. Ive tested the exact same scenario, and in fact javac dont need Module.class file (or java file) to compile Saturn.java.
BUT: after ive changed Module from interface to class, and changed in Lunar the implements to extends, magically javac is no more happy compiling Saturn.java without Module.class (Saturn.java:5: cannot access apollo.modules.Module class file for apollo.modules.Module not found)
My problem is not this second situation, im fairly comfortable with that...but why javac dont need Module.class as it is an interface?

Can someone tell me what is going on here...?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class path query.