| Author |
Classpath Trouble
|
Emmanuel Brown
Greenhorn
Joined: Apr 16, 2006
Posts: 5
|
|
Hello Everybody, Struggling with inheritance and access specifiers. I have this directory in my classpath c:\bluej\code. In c:\bluej\code\relations\family, I have two files Parent.java and Child.java In Parent.java package family; public class Parent{ protected String name = "Emmanuel Brown"; } In Child.java package family; import relations.family.Parent; public class Child extends Parent{ public static void main{String[] args){ System.out.println(new Child().name); } } Error Mesage: When I try to compile my Child.java file it says. Child.java:2: cannot access relations.family.Parent bad class file C:\bluej\code\relations\family\Parent.class class file contains wrong class: family.Parent ....... Please anyone let me know what am doing wrong. Thanks Emmanuel Brown Waltham, MA
|
 |
Ritesh Srivastava
Greenhorn
Joined: Jul 06, 2005
Posts: 26
|
|
for classpath=c:\bluej\code and Files in directory structure c:\bluej\code\relations\family Parent.java should be package relations.family; public class Parent{ protected String name = "Emmanuel Brown"; } Child.java should be package relations.family; import relations.family.Parent; public class Child extends Parent{ public static void main{String[] args){ System.out.println(new Child().name); } } Now try compiling
|
 |
Emmanuel Brown
Greenhorn
Joined: Apr 16, 2006
Posts: 5
|
|
Hi Ritesh, Many thanks. It now compiles. But I can't run the Child.class using: java Child. It throws an exception: NoClassDefFoundError: Child(wrong name: relations/family/child). Any suggestions. Emmanuel Brown Waltham, MA
|
 |
Ritesh Srivastava
Greenhorn
Joined: Jul 06, 2005
Posts: 26
|
|
You have to run the Child class by its full name java relations.family.Child
|
 |
Girish Nagaraj
Ranch Hand
Joined: Apr 19, 2006
Posts: 153
|
|
Try running this way.... relations.family.Child If ur using package statements in ur pgm, why don't you use :- javac -d . Child.java(in c:\bluej\code\relations\family). Compiler will create directories for you, Instead of you doing it.... Refer javadoc(These things are explained their in detail).
|
 |
Emmanuel Brown
Greenhorn
Joined: Apr 16, 2006
Posts: 5
|
|
Thanks guys. I got it. Emmanuel Brown Waltham, MA
|
 |
 |
|
|
subject: Classpath Trouble
|
|
|