• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem on extending a class declared on the same package!!

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am testing a code for checking default member access.
the two files are as follows

Parent.java


Child.java


"the default members are visible to subclasses only if those subclasses are on the same package as the superclass"

Here i have put the super and the sub classes in the same package
But Child.java gives me a compilation error as follows:


Child.java:3: cannot find symbol
symbol: class Parent
class Child extends Parent
^
Child.java:12: cannot find symbol
symbol : variable x
location: class cert.Child
System.out.println("x is"+x);
^
2 errors

what might be the minor problem in this???
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I think you can ignore the error message about not being able to find x -- and the "checking default member access" stuff at the moment -- and concentrate on the first error.

Child.java:3: cannot find symbol
symbol: class Parent
class Child extends Parent
^



As it can't even find the Parent class. So... How are you compiling the classes? Where are files located? From which directory are you compiling from? The classpath? etc.

Henry
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at your system's classpath. Its not even finding the parent class.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure that you compile the Parent.java before Child.java and the class file is present in the folder that is included in your classpath.
Else modify the classpath to include the folder containing your Parent.class. If you are compiling the program from command prompt You can also try the following to compile it

javac -classpath [the path to the folder containing the Parent.class] Child.java

And resolving the first error should automatically resolve the second one.

All the best!!!

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i put both the parent and child .java files in 'c drive' then i compiled the parent with commaned
javac -d . Parent.java
then child
javac -d . Child.java
then i tried to run the child class
java Child
then it gave the error like this


Exception in thread "main" java.lang.NoClassDefFoundError: Child
Caused by: java.lang.ClassNotFoundException: Child
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)



ok then i moved to cert from c
c:\ >cd cert
c:\cert>java Child
then i got the error like this

Exception in thread "main" java.lang.NoClassDefFoundError: Child (wrong name: ce
rt/Child)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)



can any one explain what is the problem. how can i run the child class?

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ok then i moved to cert from c
c:\ >cd cert
c:\cert>java Child
then i got the error like this


You don't want to execute the class Child. There is actually only a class cert.child (and cert.parent).
Go back to c:\ and try to execute cert.child from there. Check if your current directory is in your
classpath (current dir = .).

cheers
Bob
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bob Wheeler wrote:

your current directory is in your
classpath (current dir = .).

cheers
Bob


Hi Bob,
my current directory is in class path only
when i use echo %classpath% it shows
c:\program files\java\jdk1.5.0\bin;.;c:\;
but my .class files in c:\cert
i set that path also in classpath
even i am getting the same problem

please anyone help me

 
Vidya Ramakrishnan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my current directory is C:\vidya\java\cert
and i have the class files in the above directory itself.
I set the classpath also.
my Parent.java file compiles fine and i have the Parent.class file.
But after this i tried to compile Child.java,i am getting the same error as before like:

C:\vidya\java\cert>javac Child.java
Child.java:3: cannot find symbol
symbol: class Parent
class Child extends Parent
^

is there any other methodd to compile the file?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

C:\vidya\java\cert>javac Child.java



If your classpath is not set, then your current directory need to be the one use for the classpath -- which since the package is named cert, you need to do it like this...

C:\vidya\java>javac cert/Parent.java
C:\vidya\java>javac cert/Child.java

Go up one directory, and compile specifying the cert directory.

I set the classpath also.



If your classpath is set, then it should work... but you need to have that classpath set to the top of the root of the class files... meaning that you can't include the "cert" part.



And of course, after you compile it, you need to run it like so...

C:\vidya\java>java cert.Child

Notice that you the name of the class is the fully qualified named -- "cert.Child".

Henry
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
compile your two java files using these commands.
javac -d . parent.java
javac -d . child.java
and run from out side of the package directory using
java cert.child
 
NagarajGoud uppala
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,
thanks for your explanation.we can't run by moving to cert directory?
i mean like this
c:\>cd cert
c:\cert>java Child
even if i run it's giving problem.so we need to run the classes with absolute package name? am i right?
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

NagarajGoud uppala wrote:Hi Henry,
thanks for your explanation.we can't run by moving to cert directory?
i mean like this
c:\>cd cert
c:\cert>java Child
even if i run it's giving problem.so we need to run the classes with absolute package name? am i right?


Yes, because you don't want to execute the class Child. You do want to execute the class cert.Child, that is a very big difference.
Also the packages should map the directory structure. That is a very strict policy.

cheers
Bob
Ps. if I am allowed to answer.
 
Vidya Ramakrishnan
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your replies...

Got it solved like this...
Iset the classpath up to the root folder C:/vidya/java
then compiled it as follows:

javac cert/Parent.java
javac cert/Child.java

then run the Child.java file as: java cert.Child


I was unaware of setting the classpath...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//This is A.java
package p1;
public class A{
int a=5;
public int b=20;
int c=56;
protected int d=67;
}

//This is B.java
package p1;
class B extends A{
B(){
System.out.print(a+" "+b+" "+c+" "+d);
}
}

When i Execute I got the Following Error please Help me out

Errors:
C:\Users\Spurgeon\Desktop\prakku>javac p1/A.java

C:\Users\Spurgeon\Desktop\prakku>javac p1/B.java

C:\Users\Spurgeon\Desktop\prakku>java p1.B
Error: Main method not found in class p1.B, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

C:\Users\Spurgeon\Desktop\prakku>java p1/B
Error: Main method not found in class p1.B, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
 
reply
    Bookmark Topic Watch Topic
  • New Topic