File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ch10 q11 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 "ch10 q11" Watch "ch10 q11" New topic
Author

ch10 q11

zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56
Given the following directory structure:
test - |
| - GetJar.java
|
| - myApp - |
| - Foo.java



if the current dir is test, which set(s) of commands will successfully compile the two .java files and produce the output 8?

the answer is
javac myApp/Foo.java
// put into "test" a jar file that contains myApp/Foo.class
javac -classpath MyJar.jar GetJar.java
java GetJar

I don't understand why it could work.
I think it is necessary to add the statement "import myApp.*" to the file GetJar.java
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3837

ch10 q11


. You should tell the book/URL as well


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
Well....have you tried following the steps? If you had, from the code given, you would find out that you are correct. You need to have an import statement.

And please use a meaningul title - your question is about class visibility, not "ch10 q11" in some unknown book.


SCJP6
Colin Lennon
Greenhorn

Joined: Jul 16, 2009
Posts: 7
Hi everyone,

I'm a bit confused about this one - even with the 'import' statement added. The last line of the answer is

java GetJar

So we're trying to pass the GetJar.class file, which is in the current directory, to java.exe. But according to the K&B book and everything else I've read, java.exe does not look in the current directory by default, so would we not need to say

java -cp . GetJar

to tell it where to get the GetJar.class file?

Thanks!
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
The question is from the Chapter 10 Selft Test of K&B (SCJP6, page 817)

That's not corrent I am afraid, if no classpath is defined at the command line, then the classpath is assumed to be the current directory (.). If, however, you define a classpath, the current directory (.) will be ignored unless it is specifically added to the classpath.
java command (Under standard options)

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

You should use -cp or -classpath for java and and -classpath for javac. -cp is not guaranteed for javac


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Colin Lennon
Greenhorn

Joined: Jul 16, 2009
Posts: 7
Jason Irwin wrote:The question is from the Chapter 10 Selft Test of K&B (SCJP6, page 817)

That's not corrent I am afraid, if no classpath is defined at the command line, then the classpath is assumed to be the current directory (.). If, however, you define a classpath, the current directory (.) will be ignored unless it is specifically added to the classpath.
java command (Under standard options)



Thanks for your answer Jason, I think I get it now!
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

I have no classpath specified and I can run xxx.class like that:
java xxx
Why?
Colin Lennon
Greenhorn

Joined: Jul 16, 2009
Posts: 7
Lukas Smith wrote:I have no classpath specified and I can run xxx.class like that:
java xxx
Why?


Have a look at the link Jason posted above. If you don't specify a classpath using -classpath or -cp and you do not have a classpath environment variable set, then java will look in the current directory by default. I didn't realise this either, which is what was confusing me.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

OK and this is system independent?

In K&B book was written that javac searches in current directory by default but java - NO.
Colin Lennon
Greenhorn

Joined: Jul 16, 2009
Posts: 7
The link Jason posted was for Solaris - here's the corresponding doc for Windows. It looks like it is the same.

The info in the K&B book confused me as well. As I understand it, javac only searches the current directory for *.java files. When it comes to *.class files and *.jar files, both java and javac behave in the same way. Hopefully someone else can confirm if this is correct though.

zheng li
Ranch Hand

Joined: Jun 16, 2009
Posts: 56
one more question

though
java GetJar
works,
I think the classpath should be added.
java -classpath MyJar.jar GetJar

why does the classpath not needed?
Colin Lennon
Greenhorn

Joined: Jul 16, 2009
Posts: 7
Hi Zheng,

We're in the 'test' directory, and when we say

java GetJar

we are telling java to execute the file 'GetJar.class' which is in the current directory so will be found. All we need to worry about from the command line is that java can find the class file we're trying to execute.

Within the GetJar class we have included an import statement -

import myApp.*;

which will include the MyJar.jar file (although as you have found, this import statement was incorrectly left out in the book). So this is where we make sure that our class can access and use the classes within the jar file.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ch10 q11