• 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: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then how else can i extend the Fruit class which is in the food package?
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
Classpath was the problem
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic