File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with packages

 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a directory in my C drive with the name food.
In this directory I have made a class called Fruit.java such as

package food;
public abstract class Fruit
{
public abstract void methodFruit();
}

I compiled it using
c:\food>javac Fruit.java

Then i made a class in C drive with default package such as

import food.Fruit;
class Apple extends Fruit
{
public static void main(String[] args)
{
System.out.println("Apple");
}
public void methodFruit()
{
System.out.println("Fruit");

}
}

When i try to compile this file using

c:\>javac Apple.java

I get the following error

Apple.java:1: package food does not exist
import food.Fruit;
^
Apple.java:2: cannot access Fruit
bad class file: c:\food\Fruit.class
class file contains wrong class: food.Fruit
Please remove or make sure it appears in the correct subdirectory of the classpath.
class Apple extends Fruit
^

I have set the classpath as
set classpath=c:\food;
How should I correct this problem.
 
Takumi Fujiwara
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly new at java, but why did you make a package food and try to import it? Polymorphism does not require that.
 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then how else can i extend the Fruit class which is in the food package?
 
Mani Ram
Ranch Hand
Posts: 1140
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make sure that you have a dot (.) in the CLASSPATH. The dot denotes the current working directory
Modify your classpath to include the dot, using the following command, and try again.

SET CLASSPATH=.;%CLASSPATH%
 
Angela lewis
Ranch Hand
Posts: 100
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
Classpath was the problem
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic