wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpath Trouble Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpath Trouble" Watch "Classpath Trouble" New topic
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath Trouble
 
Similar Threads
problem on extending a class declared on the same package!!
package does not exist error
Code from SCJP K&B Book won't compile
Classpath
query on method over riding