• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chintan B Shah
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, did it compile? Are the class files present? The complaint is that it can't find the class files.

Henry
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chintan B Shah
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's it!

Thanks Daniel for pointing it out.

Regards,
Chintan.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic