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

Chapter 10 K&B

sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
Hi,

Is there any other material available out there that explains better than the K&B book. I'm so confused after reading this chapter. I think this chapter lacks adequate examples to support the theory. It looks like the authors were in a hurry to wrap up the book.

For example i have the follwing dir structure:


My current directory is javaExamples. I'm able to compile these two files

C:\javaExamples>javac path\foo\A.java

C:\javaExamples>javac path\boo\B.java

But i'm not able to run either of them using -cp. I tried the following:

C:\javaExamples>java path/foo/A ---> Works

C:\javaExamples>java -cp path.boo.B --> Does not work


C:\javaExamples>java path/boo/B ----> Does not work

So can someone tell me how to run B.java and also use -cp option to run the above files.

[ April 18, 2008: Message edited by: sridhar row ]

[BPSouther: Added code tags]
[ April 21, 2008: Message edited by: Ben Souther ]
vinal sen
Ranch Hand

Joined: Dec 13, 2007
Posts: 37
same boat as you.

Everything was clear till this chapter came.Only thing remaining for me to finish.

Could someone provide us some extra reference??Thank you.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The first thing to consider is that Java classes are identified by their qualified names. When you compile with a package declaration, this becomes part of the qualified name. For example, class A is declared to be in the package path.foo, so its qualified name is "path.foo.A."

The next thing to consider is the location of the class. You have a file called "A.class" under the "foo" directory. But remember, Java identifies classes by their qualified names, so this class is "path.foo.A," which is not under the "foo" directory. From Java's perspective, the class "path.foo.A" is under the "javaExamples" directory.

So if you're specifying a classpath for path.foo.A, that path should be to the "javaExamples" directory. In other words, if the current directory is "javaExamples," then the classpath to path.foo.A is simply the current directory.

In contrast, B is not part of a named package, so its qualified class name is just "B" and this is under the "boo" directory. So if the current directory is "javaExamples," then the classpath to B is path\boo.

This brings us to running B from javaExamples. Because B extends A, a classpath needs to be specified for both B and path.foo.A. So we need to specify two classpaths. One is the current directory, denoted by a dot (.), and the other is path\boo. When using the -cp flag, separate these two classpaths using a colon...

C:\javaExamples>java -cp .:path\boo B

Actually, on Windows I think you might need a semicolon instead of a colon...

C:\javaExamples>java -cp .;path\boo B
[ April 18, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
Thanks Marc for the great explanation. I was wondering if i can use -cp option with javac and i tried this

C:\javaExamples>javac -cp . path\boo\B.java

does this mean look for class files in the current directory that may be needed by B.java to compile? If it is so, do i still have to look for A.class in the current directory? and why? cos if i don't it doesn't work.

C:\javaExamples>java -cp .;path\boo B

Also, tried this and it works

C:\javaExamples>java -cp ;path\boo B (no dot just the semicolon and it works how come?)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by sridhar row:
Thanks Marc for the great explanation. I was wondering if i can use -cp option with javac and i tried this

C:\javaExamples>javac -cp . path\boo\B.java

does this mean look for class files in the current directory that may be needed by B.java to compile? If it is so, do i still have to look for A.class in the current directory? and why? cos if i don't it doesn't work...

Yes. When compiling the source file B.java, Java needs to be able to find the class path.foo.A because B extends A. Therefore, when compiling from "javaExamples," the classpath should be the current directory because that's where to find the class path.foo.A.

Important: If no classpath is specified, then Java's default is to look in the current directory. So this should compile without a classpath flag as...

C:\javaExamples>javac path\boo\B.java

If that doesn't work, this indicates that you have a classpath set somewhere else -- for example, as a system environment variable -- and that setting does not include a dot for the current directory. (Typically, the best approach is to not have any classpath set as a system variable.) In this case, you would need to specify the current directory...

C:\javaExamples>javac -cp . path\boo\B.java

Now, as to why it works to run with the semicolon (or colon) but no dot... I don't know. Apparently, "nothing" translates to the current directory, but I've never noticed this before.
Arie Prastowo
Greenhorn

Joined: Apr 20, 2008
Posts: 22
Is the order in -cp give a different?

i try java -cp path/boo;. B --> Work
but, using java -cp .;path/boo B --> throws exception nosuchmethod : main

thanks before


SCJP 5.0<br />Use the power of mind
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Arie Prastowo:
Is the order in -cp give a different?

i try java -cp path/boo;. B --> Work
but, using java -cp .;path/boo B --> throws exception nosuchmethod : main...

That doesn't make sense. The classpath tells Java where to find classes. A NoSuchMethodError is thrown when the class is found, but the method being called (in this case, main) is not in that class. I can't imagine how you're getting this error if the first case works.

(Just to be sure, I verified that either order works fine.)
[ April 21, 2008: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Chapter 10 K&B
 
Similar Threads
SCJP chapter 10 : Development Classpath problem
Ch. 10 Development question
Classpath - Question 1
Doubt in K & B pg 792 Q 5
K&B Chapter 10, Self test doubt