This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem on extending a class declared on the same package!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "problem on extending a class declared on the same package!!" Watch "problem on extending a class declared on the same package!!" New topic
Author

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

Vidya Ramakrishnan
Greenhorn

Joined: Jul 08, 2008
Posts: 14
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???
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Please look at your system's classpath. Its not even finding the parent class.


Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Jeevitha Jayaraman
Greenhorn

Joined: Oct 17, 2008
Posts: 3
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!!!

NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
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?


I know Life is very Tough...But I AM TOUGHER
SCJP 1.5
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
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

Joined: Jul 08, 2008
Posts: 14
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
Sheriff

Joined: Sep 28, 2004
Posts: 18133
    
  39

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
aswin penugonda
Greenhorn

Joined: Jun 16, 2009
Posts: 9
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


SCJP-93% (310-055)
NagarajGoud uppala
Ranch Hand

Joined: Nov 13, 2008
Posts: 86
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

Joined: Apr 24, 2009
Posts: 317
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

Joined: Jul 08, 2008
Posts: 14
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...
 
wood burning stoves
 
subject: problem on extending a class declared on the same package!!
 
Similar Threads
Code from SCJP K&B Book won't compile
cannot resolve symbol - help
default constructor must inside the Parent class. why?
Child extends Parent, Belongs to same package, Cannot access public variable
Javac cannot find the parent class