permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question No. 11- Chapter 10 K&B book SCJP 1.6 - Doubt on Answer 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 "Question No. 11- Chapter 10 K&B book SCJP 1.6 - Doubt on Answer" Watch "Question No. 11- Chapter 10 K&B book SCJP 1.6 - Doubt on Answer" New topic
Author

Question No. 11- Chapter 10 K&B book SCJP 1.6 - Doubt on Answer

Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Hi all,

The right answer to the question is provided as option A).

However, when I tried to run the program with parameters specified in option A), I got the following error message.

C:\jdk1.6\test>java GetJar
Exception in thread "main" java.lang.NoClassDefFoundError: GetJar
Caused by: java.lang.ClassNotFoundException: GetJar
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: GetJar. Program will exit.

Now, I tried various other parameters(which were not specified as options) which I thought should work and they all worked fine.

C:\jdk1.6\test>java -classpath .;MyJar.jar GetJar
8

C:\jdk1.6\test>java -classpath MyJar.jar;. GetJar
8

C:\jdk1.6\test>java -classpath . GetJar
8

So, just wanted to throw out to forum if I am understanding/implementing the question correctly or if I am doing something wrong.

For those of you who dont have book, here's the question

Q. Given the following directory structure
test |
|- GetJar.java
|- my App
- Foo.java

(Basically "test" folder contains "GetJar.java" and "myApp" folder and "myApp" folder contains "Foo.java")

And given the contents of GetJar.java and 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?

A. javac -classpath MyJar.jar GetJar.java
java GetJar

B. javac MyJar.jar GetJar.java
java GetJar

C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar

D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar

B&D are obviously wrong.

Thanks
Chintan.


SCJA 1.0
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Anybody?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

Well, did it compile? Are the class files present? The complaint is that it can't find the class files.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Can you post the contents of your jar ? It looks like you have unpacked the contents of the jar to the current folder and a compiled version of the class is available outside the jar.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Henry,
Yes. It did compile. However, it threw error at runtime.

Deepak,
I have not performed an un-jar. Actually, I created Jar and following are contents:
META-INF-->MANIFEST.FM
myApp-->Foo.java, Foo.class.

Kindly let me know if I am doing anything wrong.

Thanks
Chintan.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19003
    
  40

Chintan B Shah wrote:
Kindly let me know if I am doing anything wrong.


Well, first, can you answer my question? Was the class file created? And exactly where is it located? Next give us your file directory structure -- exactly where you are when you ran it. And the command that you ran.

We can't tell what you doing wrong, if we can't tell what you are doing.

Henry
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
Ok..let me clarify this 1 more time.

Next give us your file directory structure --


The directory structure:
C:\jdk1.6\test\GetJar.java
C:\jdk1.6\test\GetJar.class
C:\jdk1.6\test\myApp\Foo.java
C:\jdk1.6\test\myApp\Foo.class
C:\jdk1.6\test\MyJar.jar

MyJar.jar contains myApp\Foo.java and myApp\foo.class.

Was the class file created? And exactly where is it located?


Yes. The class file (GetJar.class) is created in C:\jdk1.6\test.

Here's the command used to compile GetJar.java
javac -classpath MyJar.jar GetJar.java

exactly where you are when you ran it. And the command that you ran.

Here's from where I run my code as you can see it in original post

C:\jdk1.6\test>java GetJar
Exception in thread "main" java.lang.NoClassDefFoundError: GetJar
Caused by: java.lang.ClassNotFoundException: GetJar
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: GetJar. Program will exit.


Thanks
Chintan
Daniel Negut
Greenhorn

Joined: Oct 21, 2009
Posts: 1
You probably have the CLASSPATH environment variable defined (and the test folder is not included).

Question 11 assumes that there is no CLASSPATH environment variable defined.
Chintan B Shah
Ranch Hand

Joined: Sep 23, 2008
Posts: 83
That's it!

Thanks Daniel for pointing it out.

Regards,
Chintan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question No. 11- Chapter 10 K&B book SCJP 1.6 - Doubt on Answer