• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in K&B Self Test from Last Chapter

 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please somebody answer this....pleasssssssssseeeeeee
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right.

Either
MyJar.jar should be present in classpath
OR
"java -classpath MyJar.jar GetJar" command should be used.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
noel yim
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

>
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic