• 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

Problem with packages

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone !!!

I created three Java files named Fruit,Apple and Test. All the three files are in a folder called abstract.







When I compile Test.java file an error is indicated by the cmd.

ie .\Apple.java:1: error: package food does not exist
import food.Fruit;


When package names are removed test.java is compiled with no errors and generates the desired output. Therefore I presume the problem must be with the package names or directories.
I am grateful to anyone who could help me to correct this.
Thanks !!!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gihan Madushanka wrote:Therefore I presume the problem must be with the package names or directories.


Yes, that's the problem. Your directory structure must match your package structure. So class Fruit must be in a directory named food, because it is in a package named food.

With the code as you have it above, with Apple and Test in the default package (you don't have a package statement in those source files), it should look like this:

C:\MyProject
    |
    +-- Apple.java
    |
    +-- Test.java
    |
    +-- food
        |
        +-- Fruit.java


Compile it with:

C:\MyProject> javac food\Fruit.java
C:\MyProject> javac Apple.java
C:\MyProject> javac Test.java

See: Lesson: Packages in Oracle's Java Tutorials.
 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

Gihan Madushanka wrote:Therefore I presume the problem must be with the package names or directories.


Yes, that's the problem. Your directory structure must match your package structure. So class Fruit must be in a directory named food, because it is in a package named food.

With the code as you have it above, with Apple and Test in the default package (you don't have a package statement in those source files), it should look like this:

C:\MyProject
    |
    +-- Apple.java
    |
    +-- Test.java
    |
    +-- food
        |
        +-- Fruit.java


Compile it with:

C:\MyProject> javac food\Fruit.java
C:\MyProject> javac Apple.java
C:\MyProject> javac Test.java

See: Lesson: Packages in Oracle's Java Tutorials.


Yes you are correct.
Now it compiled successfully.
Thank you very much for your great and prompt help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic