PDA

Click to See Complete Forum and Search --> : cant write my first package


vb_student
Oct 28th, 2006, 08:33 PM
Hello

I am trying to write my first package program and i have two classes Mammal.java which is like this

// Class Mammal
package Mammal;
public class Mammal {
protected String color;
public void growHair(){
System.out.println("Hair Growing");
}
}


and dog.java which is like this


//Class Dog

public class Dog extends Mammal{
private int barkFrequency;
public void bark(){
System.out.println("Dog Barking");
color = "blue";
}
}


both are in the directory E:\Work\programming\java\programs\testing\Mammal

when i do the following from the command prompt i get the responses


E:\>cd work

E:\Work>cd programming\java\programs\testing\mammal

E:\Work\programming\java\programs\testing\Mammal>javac mammal.java

E:\Work\programming\java\programs\testing\Mammal>javac dog.java
dog.java:3: cannot access Mammal
bad class file: .\Mammal.class
class file contains wrong class: Mammal.Mammal
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
public class Dog extends Mammal{
^
1 error

E:\Work\programming\java\programs\testing\Mammal>

what am i doing wrong?

CornedBee
Oct 29th, 2006, 10:50 AM
First, you didn't post the entire code of the classes, which is very wrong: it means that our answers are based on incomplete and/or incorrect information. You omitted the package declarations of the files. I assume that both files have a
package Mammal;
at the top.

Second, and this is a very minor thing, you don't follow naming convention: in Java, package names are all lowercase, i.e. "mammal".

Third, and this is the actual source of your error, Java requires the directory structure to match the package structure. That is, if you have a class Mammal in the package mammal, the runtime and the compiler both expect to find the .class file for that class to be called Mammal.class and sit in the subdirectory "mammal".
In other words, in your current setup, do every action from the "testing" directory and it should work. But that's a coincidence, because I think Mammal is actually the root of your project.

vb_student
Oct 29th, 2006, 01:22 PM
thanks for the reply cornedbee

isn't this the package declaration in the class Mammal


package Mammal;


should i repeat this in class dog

so class names start uppercase whereas package names should start lowercase

i dont understan what you mean that the directory structure needs to match the package streuvture.

my current structure is

E:\Work\programming\java\programs\testing\mammal

which has Mammal.java and Dog.java where dog's code is


//Class Dog

public class Dog extends Mammal{
private int barkFrequency;
public void bark(){
System.out.println("Dog Barking");
color = "blue";
}
}


i tried compiling from the testing directory and got the following errors

E:\Work\programming\java\programs\testing>javac dog.java
error: cannot read: dog.java
1 error

E:\Work\programming\java\programs\testing>javac mammal.java
error: cannot read: mammal.java
1 error

E:\Work\programming\java\programs\testing>cd mammal

E:\Work\programming\java\programs\testing\mammal>javac dog.java
dog.java:3: cannot resolve symbol
symbol : class Mammal
location: class mammal.Dog
public class Dog extends Mammal{
^
dog.java:7: cannot resolve symbol
symbol : variable color
location: class mammal.Dog
color = "blue";
^
2 errors

E:\Work\programming\java\programs\testing\mammal>

CornedBee
Oct 29th, 2006, 03:34 PM
isn't this the package declaration in the class Mammal
Yes. Didn't see it. Custom is to put this at the very top of the file, before any comments about the class.

should i repeat this in class dog
If you want Dog to belong to the package Mammal, of course.

so class names start uppercase whereas package names should start lowercase
That's the convention.

i dont understan what you mean that the directory structure needs to match the package streuvture.
It's all about the way Java find the .class files for the classes you load. It's a simple process once you "get" it. I've written a very long post on it some time ago. I'll see if I can find it...

OK, this contains a lot of info:
http://www.vbforums.com/showthread.php?t=294457&highlight=class+loader
Here's a condensed version:
http://www.vbforums.com/showthread.php?p=1297373&highlight=class+loader#post1297373

i tried compiling from the testing directory and got the following errors

E:\Work\programming\java\programs\testing>javac dog.java
error: cannot read: dog.java
1 error
Well, duh! There's no dog.java in the testing directory. You need to give a proper path.
E:\Work\programming\java\programs\testing>javac mammal\Dog.java
Be sure that your capitalization of directories and Java files matches that of the package names and classes. On a case-insensitive system like Windows it doesn't matter, but someone trying to compile the program on a case-sensitive system such as Linux would get a nasty surprise. Remember: Java is supposed to be cross-platform, so there's a few things you need to watch out for to make sure your programs are.

vb_student
Oct 29th, 2006, 04:26 PM
thanks for the tips dude

i have finally got it to work

i put the Mammal class and the Dog Class in a director called Animal. Animal directory is a sub-directory of testing.

put in the code package animal; in each class (mammal, dog)

and compiled the code from the parent directory (testing) using the classpath

i guess this would create the package animal, and if i had a variable say feet in mammal which has the qualifier protected, then it can only be accessed by dog since it is inside the package animal but not by any class which is outside the package animal

vb_student
Oct 29th, 2006, 06:31 PM
hey, i just tried to access a protected variable fro mthe application class (Packaging.java) where the application class, superclass and subclass are part of a package (Animal). but i can access the protected variable(defined in the superclass) only form the subclass not fromt the application class.


is that the case, that one cannot access a protected variable from the appliction class even though it is apart of the same package?

my code for the three classes is



import animal.*;
//Class Dog
public class Dog extends Mammal{
private int barkFrequency;
public void bark(){
System.out.println("Dog Barking");
color = "blue";
}
}



// Class Mammal
package animal;
public class Mammal {
protected String color;
public void growHair(){
System.out.println("Hair Growing");
}
}



import animal.*;
public class Packaging {
public static void main (String args[]){
Mammal mammal = new Mammal();
mammal.growHair();
mammal.color="blue";
}
}


i am getting the following error

E:\Work\programming\java\programs\testing>javac -classpath . ./animal/Dog.java

E:\Work\programming\java\programs\testing>javac -classpath . ./animal/Packaging.
java
./animal/Packaging.java:6: color has protected access in animal.Mammal
mammal.color="blue";
^
1 error

E:\Work\programming\java\programs\testing>

CornedBee
Oct 30th, 2006, 03:45 AM
I see no package directive in Packaging, so it is not, in fact, part of the animal package. That's why you can't access color.

vb_student
Oct 30th, 2006, 06:11 AM
thanks for the reply cornedbee

isn't the class packaging importing class animal through the use of the code


import animal.*


and according to this statement

Package declaration

The first statement, other than comments, in a Java source file, must be the package declaration.

Following the optional package declaration, you can have import statements, which allow you to specify classes from other packages that can be referenced without qualifying them with their package.

on this site http://leepoint.net/notes-java/language/10basics/import.html

i should be able to reference classes from the animal package

CornedBee
Oct 30th, 2006, 06:16 AM
And you are. But you are not in the package, so you have no access rights to protected variables.