• 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

extending class: cannot find symbol ?

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kiley, welcome to CodeRanch!

Did you set the CLASSPATH?
https://coderanch.com/how-to/java/HowToSetTheClasspath
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac Unicycle.java?
 
Kiley smith
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac Unicycle.java yields the error.

javac Cycle does not.

same folder / package.

Any ideas?
 
Kiley smith
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when compiling Unicycle.java, javac should know where the Cycle.java/Cycle.class is.
 
Kiley smith
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or,

javac has an option "-cp"/"-classpath": "Specify where to find user class files and annotation processors". (javac -help)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kiley smith
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perfect, that's exactly what I needed! I also needed that explanation. This is great.

Thank you so much!
Cheers,
Kiley
 
reply
    Bookmark Topic Watch Topic
  • New Topic