wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubts over Q.11 from Chapter 10 fron Kathy Seira Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubts over Q.11 from Chapter 10 fron Kathy Seira" Watch "Doubts over Q.11 from Chapter 10 fron Kathy Seira" New topic
Author

Doubts over Q.11 from Chapter 10 fron Kathy Seira

waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

I think there are some errors in the below question from the chapter Development:

Given the following directory structure:


When I went through the question I initially marked the option C as correct but when I looked at the answer it says A is correct, I compared options A and C and had a feeling that A can't be correct if C is not right because if we just try to execute java GetJar the jvm wont be able to find GetJar.class as java command doesn't look into the current directory by default. We would have to execute java -classpath .; GetJar instead. And when I ran the code it confirmed my feeling. So None of the given options seem correct to me.
Please correct me if I am wrong.

Thanks in advance,
Waq
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2563
    
    9

Hi there

Given the question, choice A is indeed correct. Why? First look more closely to the location of the GetJar.java and MyJar.jar files.

Choice B and D have incorrect syntax for javac so the class file for GetJar can't be generated, I assume you understand that.

Choice C javac is ok, for java command if classpath option is included, it will try to access the GetJar class file inside the MyJar.jar file, which it's not there. If you run it, it will generate a NoClassDefFoundError. Therefore, no need to include the classpath option.

Beware that the -classpath option for javac and java commands are 2 different thing.

Hope you understand it better.



[test_dir.png]


[Thumbnail for terminal.png]



K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 780
Rules:
javac -classpath (the supporting classes directories or jar) (the java file you want to compile)

java -classpath (the directory where your class file to be executed) (your class file to be executed)

java (your class file to be executed) , java looks at the current directory fo file your class file to be executed.

Hope this help.
waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

K. Tsang wrote:Hi there

Given the question, choice A is indeed correct. Why? First look more closely to the location of the GetJar.java and MyJar.jar files.



Still cant get it to run through any of the options.

As you look in the first attached image of my directory structure, I have included the files as described in the question.

As you can see in Console1. png, when I try to compile and run it as option A, it compiles fine but doesn't execute successfully and gives 'Exception in thread "main" java.lang.NoClassDefFoundError: GetJar'.

But as you can see in the third attachment - Console 2.png, I try to run it by 'java -classpath .; GetJar', it runs successfully and returns the result 8.

Please let me know accordingly.

-Thanks


[Thumbnail for Directory.png]

[Thumbnail for Console1.png]

[Thumbnail for Console 2.png]

waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

Any comments guys???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18914
    
  40

waleed qureshi wrote:
As you can see in Console1. png, when I try to compile and run it as option A, it compiles fine but doesn't execute successfully and gives 'Exception in thread "main" java.lang.NoClassDefFoundError: GetJar'.


This will look for the GetJar class file in the MyJar.jar jar file. Do you have a MyJar.jar file in the current directory? And does this jar file have a GetJar.class file in it?

waleed qureshi wrote:
But as you can see in the third attachment - Console 2.png, I try to run it by 'java -classpath .; GetJar', it runs successfully and returns the result 8.


This will look for the GetJar class file in the current directory. Since it ran, I guess you can assume that the file exists.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

none of the answer is right. it has been posted in errata. further this link will help

http://www.coderanch.com/t/270643/java-programmer-SCJP/certification/Error-Sun-Certified-Programmer-Java
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

also keep one thing in mind that the question assumes that you don't have classpath environment variable set. according to the directory structure YOU HAVE GIVEN even java GetJar will work . why ?

since in your figure i can see that you have created MyJar.jar out of myapp directory(which contains Foo class). you haven't deleted myapp directory. so when you do java GetJar it will by default look in the current directory(as opposed to what you said - keep in mind that by default java , javac will look for classfile or sourcefile in current directory) and will find myapp.Foo.

now suppose you delete the folder myapp and just keep the MyJar.jar (which is actually what the authors intended). now if you do java GetJar , it will give Error that it can't find the Foo class file. why ? java binary looks in the current directory for any CLASS FILE NOT JAR FILE. so you have to explicitly include the path of the jar file up til the name of jar file in your classpath like this :

java -classpath MyJar.jar;. GetJar (current directory is included in the classpath because it gets overriden once you specify the classpath ). this will run again

so considering your directory structure(which has myapp folder) both java GetJar and java -classpath MyJar.jar;. GetJar will work.

java GetJar didn't worked on your system because i think you might have set environement variable in your system.


Still in the end none of the options given in the book are correct. the only correct answer is java -classpath Myjar.jar;. GetJar

i hope you are clear. if you are still i doubt please feel free to post
waleed qureshi
Ranch Hand

Joined: Apr 17, 2012
Posts: 46

gurpeet singh wrote:

keep in mind that by default java , javac will look for classfile or sourcefile in current directory



But I think the authors have mentioned in the book that by default javac looks into the current directory but java doesn't, you have to explicitly specify it. So could you please correct me if I am wrong on this?

And yeah I am clear about the question now, that was my original post that I believed none of the answers are right. Thanks for clearing that and sharing the errata link.

Waq
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

waleed qureshi wrote:
gurpeet singh wrote:

keep in mind that by default java , javac will look for classfile or sourcefile in current directory



But I think the authors have mentioned in the book that by default javac looks into the current directory but java doesn't, you have to explicitly specify it. So could you please correct me if I am wrong on this?

And yeah I am clear about the question now, that was my original post that I believed none of the answers are right. Thanks for clearing that and sharing the errata link.

Waq


hi waleed.

java and javac binaries DO look in current directory for classfiles by default. by default means that you HAVEN'T set classpath environement variable. lets take an example. suppose i have a source file Test.java


public class Test
{


}

the file is saved in c://com/guru/Test.java. also note that i don't have any classpath environment variable set. you can check your classpath by using echo %classpath% on windows or echo $classpath on nix machine.

my current directory say is root c://com/guru. i compile it using
javac com/guru/Test.java // compiles and creates Test.class in c://com/guru

now let us run it using
java Test // it will run normally. why ? java looks into current directory(which is c://com/guru) and will find Test.class there.

now here is a gotcha. suppose you try to run the program by specifying classpath like shown below :
java -cp c://ranch Test // the -cp switch OVERRIDES the default setting of java(which is to look in current director) and will try to search for class files in c://ranch and won't be able to locate Test there. to solve this we have to do

java -cp c://ranch;. Test // we have to manually add current directory in classpath .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubts over Q.11 from Chapter 10 fron Kathy Seira