GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chap.10 Development , Q5 pg813 : not understood answer and cannot run code 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 "Chap.10 Development , Q5 pg813 : not understood answer and cannot run code" Watch "Chap.10 Development , Q5 pg813 : not understood answer and cannot run code" New topic
Author

Chap.10 Development , Q5 pg813 : not understood answer and cannot run code

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I tried to implement that question by making the necessary files and directories. But failed to run the code as indicated in the question.
[EDIT] I am using windows 7-64 bit
The question :

5. If three versions of MyClass.class exist on a file system:
Version 1 is in /foo/bar
Version 2 is in /foo/bar/baz
Version 3 is in /foo/bar/baz/bing

And the system's classpath includes
/foo/bar/baz

And this command line is invoked from /foo
java -classpath /foo/bar/baz/bing:/foo/bar MyClass

Which version will be used by java?

A. /foo/MyClass.class
B. /foo/bar/MyClass.class
C. /foo/bar/baz/MyClass.class
D. /foo/bar/baz/bing/MyClass.class
E. The result is not predictable

The answer is D. Answer: blah...blah...and java uses the first match it finds.

The first match is the first classpath specfied after java---- command ? If i reverse the order given above, will the answer be B ?
I cannot verify this for myself because i keep getting an error when i issue this command
[EDIT] changing the : to a ; also did not help.


I made the source code of each MyClass and mentioned the folder it is inside using print statements. Here is what each file looks like :


Why am i failing ???


SCJP 6. Learning more now.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Rahul Sudip Bose wrote:
C:\run code\foo>java -classpath \foo\bar\baz\bing:\foo\bar MyClass

Ikpefua wrote:


Hello Rahul, when you decalre a class in a package you MUST tell java the fully qualified name (bar.MyClass), and if you are invoking from the immediate super directory in this case foo you MUST use the dot (.) to tell java to search current directory as follows:

C:\run code\foo>java -classpath . bar.MyClass

Invoke the java command EXACTLY as specified above, and it should work.

I Hope This Helps.



OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Ikpefua Jacob-Obinyan wrote:
C:\run code\foo>java -classpath . bar.MyClass
Invoke the java command EXACTLY as specified above, and it should work.


It seems you have misunderstood what i said. Now i remove all package statements from all versions of MyClass (there are three versions of
MyClass just like before). All are the same, except that they print the folder that they are in. I still get errors after invoking the command :

C:\run code\foo>java -classpath \foo\bar\baz\bing:foo\bar MyClass

Here is my directory structure : (i think its ok)


Here is the error after invoking java :



Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rahul Sudip Bose wrote:
C:\run code\foo>java -classpath \foo\bar\baz\bing:foo\bar MyClass


are you running the program in linux operating system? (and)
<edit>you need to specify the class name with *package name*[fully qualified name] example: java -classpath \foo\bar\baz\bing:foo\bar foo.bar.MyClass</edit>
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I am running windows 7-64 bit and there are no package statements in my program. I removed them and compiled the code.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

ok, then I am not sure about windows 7.

note: in windows xp, if you use 2 class path for example java -classpath path1; path2 Test

and if both path1 and path2 has same version of class then first specified(path1) one wins the race!
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Seetharaman Venkatasamy wrote:
and if both path1 and path2 has same version of class then first specified(path1) one wins the race!


That is exactly what i am trying to verify. For that purpose i made 3 versions of MyClass.

Still no luck with the ; Got the same error :
C:\run code\foo>java -classpath \foo\bar\baz\bing;\foo\bar MyClass




Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Rahul let us move step by step Did you use the 'javac' command to compile your class 'MyClass' ??? If Yes Tell Me The Exact Steps You Followed... REMEMBER That a .java file Must Be Compiled First to a .class file Before You Can Run It With the 'java' command. I await your reply.
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
"The answer is D. Answer: blah...blah...and java uses the first match it finds. "

It's an "Absolute" path, it doesn't matter what directory you're in when you see a path started with a slash (/). Hope this help...

Java rules from K & B book: An absolute path in Unix begins with a forward
slash (/) (on Windows it would be something like c:\). The leading slash indicates
that this path is starting from the root directory of the system. Because it's starting
from the root, it doesn't matter what the current directory is—a directory's absolute
path is always the same.

Since absolute paths are specified the search results will always be the same. Specifically,
only "bing" will be searched, regardless of the current directory.


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Tommy...Thanks for the information, Rahul want to compile and run the class and I want to show him the steps.
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

My directory structure is ok...I am inside foo for execution - ok...no package statements in class files - ok...typed command in book with windows separators \ and ; --ok...
but still there is an error...i guess i will have to keep this aside for now and move ahead.

here is what i have been doing :
C:\run code\foo>java -classpath \foo\bar\baz\bing;\foo\bar MyClass


Before i proceed ahead can someone confirm this : If the compiler finds the desired class file in the 1st classpath itself, it wont look further ???

thanks to all.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rahul Sudip Bose wrote:My directory structure is ok...I am inside foo for execution - ok...no package statements in class files - ok...typed command in book with windows separators \ and ; --ok...
but still there is an error...i guess i will have to keep this aside for now and move ahead.

here is what i have been doing :
C:\run code\foo>java -classpath \foo\bar\baz\bing;\foo\bar MyClass


Before i proceed ahead can someone confirm this : If the compiler finds the desired class file in the 1st classpath itself, it wont look further ???

thanks to all.


execute this: C:\run code>java -classpath .\foo\bar\baz\bing;.\foo\bar MyClass . not tested though
<edit>
add period(.) operator before \foo\bar\baz\bing and \foo\bar.
[or] remove the \ and just use foo\bar\baz\bing and foo\bar.
</edit>
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Seetharaman Venkatasamy wrote:
execute this: C:\run code>java -classpath .\foo\bar\baz\bing;.\foo\bar MyClass . not tested though
<edit>
add period(.) operator before \foo\bar\baz\bing and \foo\bar.
[or] remove the \ and just use foo\bar\baz\bing and foo\bar.
</edit>



Thanks ! that works...what a relief.

Here are the test results :

without one dot :


with dot :


Now can someone tell me whats happening... ???
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rahul Sudip Bose wrote:
Now can someone tell me whats happening... ???

case 1:
with out ( . ) the first class path is invalid and 2nd path is valid[since you have mentioned .] so you are getting *main of my class in \foo\bar*

case 2: both path are valid. so first one wins, thus result : *main of my class in \foo\bar\baz\bing *
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

the book clearly says :
And this command line is invoked from /foo
java -classpath /foo/bar/baz/bing:/foo/bar MyClass

i did that from foo> (using windows separators), but it didnt work. So should they have printed "from run code" or whatever your folder name is ?

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Rahul Sudip Bose wrote:the book clearly says :
And this command line is invoked from /foo
java -classpath /foo/bar/baz/bing:/foo/bar MyClass

i did that from foo> (using windows separators), but it didnt work. So should they have printed "from run code" or whatever your folder name is ?



Ikpefua wrote:


Rahul I have done everything as explained and it worked for me! now lets do it like this:
_______________________________________________________________________________
In the command line do this:
cd Foo

or

cd C:\Foo
________________________________________________________________________________

You MUST see something like this:
C:\Foo>
________________________________________________________________________________

Now invoke the 'java' command from (C:\Foo>)EXACTLY like this:
C:\Foo>java -classpath Bar\Baz\Bing;Bar\Baz MyClass
________________________________________________________________________________

The above MUST run Correctly, If not we will have to know EXACTLY how your class file reflect in
the directories. And If your environment variables etc are properly set.
________________________________________________________________________________

Take Note: Of The White Spaces; java-space--classpath-space-Bar\Baz\Bing;Bar\Baz-space-MyClass

Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

I tried that this :


From the last trial, it seems that dot. is a start point and ; is an end-point to tell the jvm where to start searching and when to stop. Is my understanding correct ???

But look at the second trial...looks like the jvm just likes . and ;


Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Rahul Java

From the last line, it seems that dot. is a start point and ; is an end-point to tell the compiler where to start searching and when to stop. Is my understanding correct ???
Your understanding is wrong, here is the explanation:

The dot (.) tells java to search the current directory.

The (;) is simply used in windows to separate directory paths.

Take note that at this stage everything we talk about is 'java' Runtime
and NOT 'javac' compile time, the compiler has already done its job by
converting your (.java file) to a (.class file).
_____________________________________________________________

C:\run code\foo>java -classpath \bar\baz\bing\ MyClass

The above command statement is WRONG and this is RIGHT:

C:\run code\foo>java -classpath bar\baz\bing MyClass
_____________________________________________________________

STUDY CAREFULLY both commands, word for word, character for character
Please reply me with the difference that you noticed in both commands.
_____________________________________________________________
Rahul Sudip Bose
Ranch Hand

Joined: Jan 21, 2011
Posts: 637

Ikpefua Jacob-Obinyan wrote:@Rahul Java
From the last line......in both commands.


I tried this, as you mentioned :



Thanks for pointing out my errors
This chapter is simply irritating. I seems that no serious developer uses CMD, so i thought that skimming this chapter would be fine...but no
Back to the first page now...

regards
rb

Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Rahul Sudip Bose wrote:
This chapter is simply irritating. I seems that no serious developer uses CMD, so i thought that skimming this chapter would be fine.


Ikpefua wrote:

Just like fellow rancher Mala says: "You should always find out the meaning and use of methods", same goes with the CMD, have you thought of the command line saving you the 'Headache' of Modifying codes each time you want to run a program. E.g lets say you create a program that requires specific arguments to give you a specific result, Will you have to write an entire program for each new argument?. Summary, CMD is FUNDAMENTAL, and the exam creators are aware of that, it makes sense that they added the topic.
fadi aboona
Ranch Hand

Joined: Apr 25, 2010
Posts: 71
this chapter is frustrating the * out of me, i have been on this chapter few days now, researched other books, googled but every exercise i do i get more confused and frustrated....

this was taken from this book "A Programmer’s Guide to Java™ SCJP Certification" which is a great book.

"It is very important to understand that when we want the JDK tool to search in a named package, it is the location of the package that is specified, i.e., the class path indicates the directory that contains the first element of the fully qualified package name."

so,

should return


the classpath would look for package "foo.bar.baz.bing" under /foo/bar/baz/bing which will not find but it would find the package baz under /foo/bar

1. What do you think?
2. shouldn't this question indicate what package MyClass is under?

p.s, sorry if it looks like i'm hijacking this post from OP, please tell me if i need to create a new post.


Thanks,
 
GeeCON Prague 2014
 
subject: Chap.10 Development , Q5 pg813 : not understood answer and cannot run code