Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with packages

 
Gihan Madushanka
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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 !!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic