• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a package from another file  RSS feed

 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the code above is stored as Animal.java




the code above is stored as Main.java.

but when i compile Main.java, i get an error saying that Animal is not accessible. They are both stored in the same directory.

Please tell what my mistake is.
 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your mistake is that they're in the same directory.

Animal needs to be in directory G, which is a subdirectory of the directory Main is in.
 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the issue was solved but all the 3 files below are in the same directory inside the same project..so i dont understand what the post above actually says..

code below is stored in Animals.java .



code below is stored in Elephant.java .



code below is stored in Main.java .

 
Stephan van Hulst
Saloon Keeper
Posts: 7991
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again, classes need to be in a directory structure that reflects the package structure.

If you have a class a.b.c.Foo, then the file must be saved as a/b/c/Foo.java, relative to your project source folder.

So your classes Elephant and Animals need to be in files G/Animals.java and G/Elephant.java, while Main.java needs to be in the top directory.
 
sinatra roger
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Again, classes need to be in a directory structure that reflects the package structure.


So your classes Elephant and Animals need to be in files G/Animals.java and G/Elephant.java, while Main.java needs to be in the top directory.


thanks for the advice..i will make it a practice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!