File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ch10 q11

 
zheng li
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3908
9
Chrome Fedora Hibernate
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ch10 q11


. You should tell the book/URL as well
 
Jason Irwin
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Colin Lennon
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use -cp or -classpath for java and and -classpath for javac. -cp is not guaranteed for javac
 
Colin Lennon
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no classpath specified and I can run xxx.class like that:
java xxx
Why?
 
Colin Lennon
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
1
Android Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic