aspose file tools*
The moose likes Beginning Java and the fly likes extending class: cannot find symbol ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending class: cannot find symbol ?" Watch "extending class: cannot find symbol ?" New topic
Author

extending class: cannot find symbol ?

Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Hi there,
I have a class "Cycle", compiles just fine = javac Cycle.java so far so good

I have another class extending Cycle, it's called Unicycle. Here's where the wheels fall off:
javac Unicycle
error reads: Unicycle.. cannot find symbol .... public class Unicycle extends Cycle {

Both java files are in the same package / directory. In using Eclipse, the program builds and runs. At the Windows command line is where the error message is.


Cycle.java



Unicycle


Any help is soo much appreciated.

Kiley
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3611
    
  14

Hi Kiley, welcome to CodeRanch!

Did you set the CLASSPATH?
http://www.coderanch.com/how-to/java/HowToSetTheClasspath
xinghai huang
Greenhorn

Joined: Jun 17, 2011
Posts: 4
javac Unicycle.java?
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
javac Unicycle.java yields the error.

javac Cycle does not.

same folder / package.

Any ideas?
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Hi Stephan,

The CLASSPATH has me a bit confused.

I have entered the variable path directory to here:
C:\Program Files\Java\jdk1.6.0_22\bin

I don't know why compiling Cycle works but compiling Unicycle does not work? I haven't modified the CLASSPATH because shouldn't they both compile or both not compile?

I really do appreciate any your help.

And, you thank you for the welcome!
xinghai huang
Greenhorn

Joined: Jun 17, 2011
Posts: 4
when compiling Unicycle.java, javac should know where the Cycle.java/Cycle.class is.
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
how do i let javac know where the cycle.java/.class files are?

do have to create a CLASSPATH environment variable and provide the path to where the .java and .class files of this project are?

xinghai huang
Greenhorn

Joined: Jun 17, 2011
Posts: 4
or,

javac has an option "-cp"/"-classpath": "Specify where to find user class files and annotation processors". (javac -help)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Welcome to the Ranch.

No, don't set a CLASSPATH environment variable; if it is already set, remove it. If there is no CLASSPATH set, the Java compiler and runtime will look in the current directory for *.class files. So, suppose you have your source files in the directory C:\Project, then you should be able to compile them like this:

C:\Project> javac Cycle.java

C:\Project> javac Unicycle.java


You can also specify the classpath on the command line with the -cp or -classpath option. If, for some reason, the above doesn't work, try specifying the current directory ".":

C:\Project> javac Cycle.java

C:\Project> javac -cp . Unicycle.java


If you get any error messages, then please copy & paste the exact error message here - the more precise information we get, the better we can help you solve this.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
which way do you recommend javac should be told where the Cycle.java/Cycle.class is?

i hate to ask this but can you show me?
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Hi Jesper,

Thank you for the welcome and help!

I have removed the CLASSPATH variables from both the user variable and the system variables. Is this like editing the registry where the change is immediate or is a reboot in order? I only ask because that didn't solve it. I still get the "cannot find symbol, symbol: class Cycle".
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

You don't need to reboot, but you do need to close and re-open the command prompt window, because it will not pick up the changes immediately.
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Thanks, Jesper.

Still getting the same error. Would you grab that code and run it? I'm pretty green to java, perhaps I am doing something dumb.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

I did that, it compiled without problems. What do you get if you type in this command:

echo %CLASSPATH%

It will show you if you still have a CLASSPATH environment variable set. Also, please copy & paste the exact error messages that you get. You might be overlooking something in the error message that we would see if you show us the exact error message.
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Thank you, thank you to everyone for your help.

I had "package bike;" at the top of my classes. All the classes were in the bike package (folder) so I thought it was harmless to have it that declaration there. ugh!

I removed "package bike;" and it works, I don't know why it works without it but it does.

On a different and fantastic note, I have found this great resource and I will be returning often!

Thank you and have a great night (it's 1:30am here!)

Cheers,
Kiley
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
echo %CLASSPATH% gave me:

Program Files\Java\jre6\lib\ext\QTJava.zip



Which is very strange because I have deleted the CLASSPATH entries from both the user and system. I wonder why it is still there.

Thank you for grabbing my code and trying it. I really do appreciate it!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Ok, good that the problem is solved!

Did you close and re-open the command prompt window after removing the CLASSPATH environment variables?

If the classes are in the package bike, then you'd need to compile it from the directory that the "bike" directory is in, for example:

C:\Project> javac -cp . bike\Cycle.java

C:\Project> javac -cp . bike\Unicycle.java

Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Yahoo!!! Thank you so much. I moved up one directory and ran this as you suggested:

javac -cp . Bike\Cycle.java
javac -cp . Bike\Unicycle.java

Total success!!! I'm so happy. I'm so tired, but I'm more happy!

Thank you so much for sticking with me through this. I have learned exactly how to do this and I'm so relieved. Thank you!

To run the main program in Biking (has the main method and compiles), do I have to run from the higher folder again? I've been trying and it seems to keep barfing.
java -cp . Bike\Biking <- fails
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14111
    
  16

Yes, but you have to type it like this:

java -cp . bike.Biking

With the "java" command, you supply the complete class name (including the package name). Parts of the package name and the name of the class are separated by dots ".", not backslashes "\".
Kiley smith
Greenhorn

Joined: Jun 17, 2011
Posts: 27
Perfect, that's exactly what I needed! I also needed that explanation. This is great.

Thank you so much!
Cheers,
Kiley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extending class: cannot find symbol ?