This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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.
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.
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.
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
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.
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
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 .