• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts over Q.11 from Chapter 10 fron Kathy Seira

 
waleed qureshi
Ranch Hand
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3457
14
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 test_dir.png]
Test directory
terminal.png
[Thumbnail for terminal.png]
Terminal output
 
Himai Minh
Ranch Hand
Posts: 1294
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Directory.png
[Thumbnail for Directory.png]
Console1.png
[Thumbnail for Console1.png]
Console 2.png
[Thumbnail for Console 2.png]
 
waleed qureshi
Ranch Hand
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any comments guys???
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic