wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem with packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with packages" Watch "Problem with packages" New topic
Author

Problem with packages

Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with packages