aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B Self Test from Last Chapter 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 "Doubt in K&B Self Test from Last Chapter" Watch "Doubt in K&B Self Test from Last Chapter" New topic
Author

Doubt in K&B Self Test from Last Chapter

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

I don't have the book right now with me but I think this is the 11th question from self test from Development Chapter...

The question
This is a tree structure

test
|-GetJar.java
|-MyJar.jar
|-myApp
___|-Foo.jar
-------------------------------------------
GetJar.java contains

System.out.println(myApp.Foo.d);
-------------------------------------------
-------------------------------------------
Foo.java contains

package myApp;
public class Foo
{
public static int d = 8;
}
--------------------------------------------

The Foo.class file is in MyJar.jar.
Now the question says that change the working directory to test and run a pair of commands to get output as 8.
--------------------------------------------
The Answer in the Book-
javac -classpath MyJar.jar GetJar.java
java GetJar
--------------------------------------------

When I tried to run the code, it generates an error--> no class definition found myApp/Foo

I think that the answer is wrong. To run the GetJar the myApp folder must be in the class path.

The program runs if the Foo.class file is in myApp folder(as it should) but the question says that Foo.class is in MyJar.jar.
---------------------------------------------
According to me the correct answer is
javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
---------------------------------------------

Can anyone please tell me if I am right or wrong and if I am wrong, then why am I wrong and why is the code not running on my PC.....
[ August 30, 2008: Message edited by: Ankit Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

please somebody answer this....pleasssssssssseeeeeee
Puneet Nahata
Greenhorn

Joined: Aug 30, 2008
Posts: 9
I think you are right.

Either
MyJar.jar should be present in classpath
OR
"java -classpath MyJar.jar GetJar" command should be used.


do not anger the GODS.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

Here is the complete question


Given contents of GetJar.java

contents of Foo.java


If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar located in test, which set(s) of commands will compile GetJar.java and produce the output 8?

Correct answer
javac -classpath MyJar.jar GetJar.java
java GetJar

The question is silent about Foo.class file being in myApp directory. So I assume that it is only in the MyJar.jar file and not in the myApp directory along with Foo.java. The above code compiles successfully but it produces an exception on runtime. I feel that the second command should be

java -classpath .;MyJar.jar GetJar
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

When Foo.class is in myApp directory plain java command gives output as 8.

if you delete the file Foo.class from the myApp directory then you need to give classpath to jar file.If i give classpath to jar file then it is working fine and giving output as 8

Ankit extract your jar file and see whether you got same directory structure or not i mean Foo.class should be there in myApp folder.

otherwise you will get no package found exception
[ September 03, 2008: Message edited by: ramesh maredu ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
noel yim
Greenhorn

Joined: Sep 04, 2008
Posts: 8
I also have doubts about this question
And a further question about the .;MyJar.jar vs MyJar.jar;. unanswered.
Any suggestions?

[ September 05, 2008: Message edited by: noel yim ]
[ September 05, 2008: Message edited by: noel yim ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
Originally posted by Ankit Garg:
I feel that the second command should be

java -classpath .;MyJar.jar GetJar


I also think the command shoud be

java -classpath MyJar.jar:. GetJar
Because when the compiler searches for classfiles , by default it doesnot look in the current directory and the current directory should be explicitly specified.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
noel yim
Greenhorn

Joined: Sep 04, 2008
Posts: 8
Yes. If you read my post here
java -cp .;MyJar.jar GetJar <--- won't work
java -cp MyJar.jar;. GetJar <--- works
why?

My post has one further question about Q12, both works. Why?

Thank you very much.
Jerome Jacobsen
Greenhorn

Joined: Jan 26, 2010
Posts: 12
Hmmm. Checkout this weirdness. I thought the first compile command would work. And it seems to have worked for others in this thread. But I have to add a classpath separator even though I'm only supplying one path to the classpath. Strange.

>echo %CLASSPATH%
%CLASSPATH%

>javac -version
javac 1.6.0_12

>dir
Volume in drive C has no label.
Volume Serial Number is BCC4-9595

Directory of C:\Development\SCJP\Chapter10\test

03/11/2010 08:39 AM <DIR> .
03/11/2010 08:39 AM <DIR> ..
03/11/2010 08:27 AM 123 GetJar.java
03/11/2010 08:27 AM <DIR> myApp
03/11/2010 08:26 AM 938 MyApp.jar
2 File(s) 1,061 bytes
3 Dir(s) 27,877,011,456 bytes free

>javac -classpath MyJar.jar GetJar.java
GetJar.java:3: package myApp does not exist
System.out.println(myApp.Foo.d);
^
1 error

>javac -classpath MyJar.jar; GetJar.java

>

Interestingly this also works:
>javac -classpath . GetJar.java

>
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
I think the answer is correct. Suppose if MyJar.jar file was not created then as per question
test-|
|-GetJar.java
|
|-myApp-|
|-Foo.java
the program compiles and runs using the command:
test>javac GetJar.java
test>java Apple
I think this is because the root directory of package myApp is test. As we are running the program from test , the compiler searches the subdirectory and finds the Foo.java file. Similarly, the JVM can also find the file.

So, when we make the myJar.jar file using:
test>jar -cf MyJar.jar myApp
and compile using:
test>javac -cp MyJar.jar GetJar.java
However, to run either of the following commands work:
1. java GetJAr
2.java -cp MyJar.jar:. GetJar
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
test
|-----GetJar.java
|-----myApp-------Foo.java

test>javac GetJar.java
test>java GetJar

compiles and runs as test is root directory of package myApp

So, when we make the myJar.jar file using:

test>jar -cf MyJar.jar myApp
and compile using:

test>javac -cp MyJar.jar GetJar.java

However, to run either of the following commands work:
1. java GetJAr
2.java -cp MyJar.jar:. GetJar
J C upadhyay
Greenhorn

Joined: Nov 24, 2011
Posts: 16
J C upadhyay wrote:I think the answer is correct. Suppose if MyJar.jar file was not created then as per question
test-|
|-GetJar.java
|
|-myApp-|
|-Foo.java
the program compiles and runs using the command:
test>javac GetJar.java
test>java GetJar
I think this is because the root directory of package myApp is test. As we are running the program from test , the compiler searches the subdirectory and finds the Foo.java file. Similarly, the JVM can also find the file.

So, when we make the myJar.jar file using:
test>jar -cf MyJar.jar myApp
and compile using:
test>javac -cp MyJar.jar GetJar.java
However, to run either of the following commands work:
1. java GetJAr
2.java -cp MyJar.jar:. GetJar
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
J C upadhyay wrote:
J C upadhyay wrote:I think the answer is correct. Suppose if MyJar.jar file was not created then as per question
test-|
|-GetJar.java
|
|-myApp-|
|-Foo.java
the program compiles and runs using the command:
test>javac GetJar.java
test>java GetJar
I think this is because the root directory of package myApp is test. As we are running the program from test , the compiler searches the subdirectory and finds the Foo.java file. Similarly, the JVM can also find the file.

So, when we make the myJar.jar file using:
test>jar -cf MyJar.jar myApp
and compile using:
test>javac -cp MyJar.jar GetJar.java
However, to run either of the following commands work:
1. java GetJAr
2.java -cp MyJar.jar:. GetJar


As I remember the KB book, MyJar.jar or all other jar files should be put as the last directory in -cp. I guess it means "java -cp .;MyJar.jar GetJar"
If we put "java -cp MyJar.jar;. GetJar" may not work as MyJar.jar is not the last directory in -cp
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in K&B Self Test from Last Chapter