|
-
Oct 28th, 2006, 07:33 PM
#1
Thread Starter
Frenzied Member
cant write my first package
Hello
I am trying to write my first package program and i have two classes Mammal.java which is like this
Code:
// 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
Code:
//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?
-
Oct 29th, 2006, 10:50 AM
#2
Re: cant write my first package
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2006, 01:22 PM
#3
Thread Starter
Frenzied Member
Re: cant write my first package
thanks for the reply cornedbee
isn't this the package declaration in the class 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
Code:
//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>
-
Oct 29th, 2006, 03:34 PM
#4
Re: cant write my first package
 Originally Posted by vb_student
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.p...t=class+loader
Here's a condensed version:
http://www.vbforums.com/showthread.p...er#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.
Code:
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 29th, 2006, 04:26 PM
#5
Thread Starter
Frenzied Member
Re: cant write my first package
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
-
Oct 29th, 2006, 06:31 PM
#6
Thread Starter
Frenzied Member
Re: cant write my first package
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
Code:
import animal.*;
//Class Dog
public class Dog extends Mammal{
private int barkFrequency;
public void bark(){
System.out.println("Dog Barking");
color = "blue";
}
}
Code:
// Class Mammal
package animal;
public class Mammal {
protected String color;
public void growHair(){
System.out.println("Hair Growing");
}
}
Code:
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>
-
Oct 30th, 2006, 03:45 AM
#7
Re: cant write my first package
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Oct 30th, 2006, 06:11 AM
#8
Thread Starter
Frenzied Member
Re: cant write my first package
thanks for the reply cornedbee
isn't the class packaging importing class animal through the use of the code
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/langu...cs/import.html
i should be able to reference classes from the animal package
-
Oct 30th, 2006, 06:16 AM
#9
Re: cant write my first package
And you are. But you are not in the package, so you have no access rights to protected variables.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|