• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ch10 q11

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ch10 q11



. You should tell the book/URL as well
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

reply
    Bookmark Topic Watch Topic
  • New Topic