File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

default class accessing another default class

 
Billal Rabah
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please check out the following code:
// TestAnimal.java
package com.mycompany.animals;
class Animal{}
class Horse{}
public class TestAnimal{}

// Canine.java
package com.mycompany.animals;
public class Canine extends Animal{}

when i tried to compile Canine.java a compiler gave me an error that a class Animal is not found. I thought since both Canine and Animal in the same package then Canine can access( extends in this case) Animal since it has a default access.
Let me know what i am missing
and thanks
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Java Man,"

Welcome to JavaRanch! Please check your private messages by clicking on My Private Messages. Thanks!
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think it's an access issue.

Have you already compiled TestAnimal, so that the Animal class file exists? Or are you expecting javac to figure out that the class definition is in a file with a different name?

(If the Animal.class file does exist, then it's probably a classpath issue. Let us know...)
[ December 04, 2007: Message edited by: marc weber ]
 
Billal Rabah
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have compiled TestAnimal.java and i have all the three classes(TestAnimal, Animal, Horse)
 
Billal Rabah
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the commmands
D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes com/mycompany/animals/TestAnimal.java

D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes com/mycompany/animals/Canine.java
com/mycompany/animals/Canine.java:5: cannot find symbol
symbol: class Animal
public class Canine extends Animal{}
^
1 error

D:\Documents and Settings\GG\My Documents\My Java\source>
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like...
  • The directory "My Java" contains (at least) 2 subdirectories: "source" and "classes".
  • The current directory (the one you're compiling from) is "source".
  • "com.mycompany.animals.Animal.class" is under the directory "classes".
  • So you need to specify a classpath so that javac can find com.mycompany.animals.Animal.class. The flag for classpath is -cp (similar to the -d directory flag you're already using). So from your current directory, do you see what the classpath value should be?
    [ December 04, 2007: Message edited by: marc weber ]
     
    Billal Rabah
    Greenhorn
    Posts: 4
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks the code runs great now. Yes, it was a CLASSPATH problem. I have used the flag -cp as you instructed.
    Before
    D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes com/mycompany/animals/Canine.java
    com/mycompany/animals/Canine.java:5: cannot find symbol
    symbol: class Animal
    public class Canine extends Animal{}
    ^
    1 error
    After
    D:\Documents and Settings\GG\My Documents\My Java\source>javac -d ../classes -cp ../classes com/mycompany/animals/Canine.java

    D:\Documents and Settings\GG\My Documents\My Java\source>

    Appreciate it man
     
    marc weber
    Sheriff
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Glad it works!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic