This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath Trouble

 
Emmanuel Brown
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to run the Child class by its full name

java relations.family.Child
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I got it.
Emmanuel Brown
Waltham, MA
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic