cannot find symbol Error in Java
I have 3 class file, when i compile the class which extends the other class gives compilation error like symbol not found.
public class Animal < public static void hide() < System.out.println("Hide Method Of Animal"); >public void override() < System.out.println("The Override method of Animal"); >>
public class Cat extends Animal < public static void hide() < System.out.println("Hide Method Of Cat"); >public void override() < System.out.println("The Override method of Animal"); >>
TestAnimal.java:6: cannot find symbol symbol : class Cat location: class com.Test.TestAnimal Cat myCat = new Cat(); ^
You should publish show us your packages, directory structure and the way you invoke your java application.
2 Answers 2
Yes, see if there is a .class file for each .java file after you compile. If not, make sure that each one has a .class file.
When you run, use the -classpath argument to point to the directory where all the .class files live.
You sound like you could use some instruction on compiling and running Java.
Here’s the spoon feeding answer:
- Go to your d:\Mine\CoreJava\Programming directory in a command shell.
- Create a directory /classes
- Compile by typing javac -d classes -cp classes src/*.java
- You should see all the .class files created at once.
- Run by typing java -cp .;classes FullClassNameOfYourMain
Right — Cat depends on Animal, but the compiler has no way to find it because you didn’t tell it about the Animal.class file. Use the -classpath argument when you compile, too.
There’s absolutely nothing wrong with your code. Here’s what I’ve done. See if you did everything right:
$ cat > Animal.java public class Animal < public static void hide() < System.out.println("Hide Method Of Animal"); >public void override() < System.out.println("The Override method of Animal"); >> $ cat > Cat.java public class Cat extends Animal < public static void hide() < System.out.println("Hide Method Of Cat"); >public void override() < System.out.println("The Override method of Animal"); >> $ cat > TestAnimal.java public class TestAnimal < public static void main(String args[]) < Cat myCat = new Cat(); Animal myAnimal = (Animal)myCat; myAnimal.hide(); myAnimal.override(); >> $ javac TestAnimal.java $ ls Animal.class Animal.java Cat.class Cat.java TestAnimal.class TestAnimal.java $ java TestAnimal Hide Method Of Animal The Override method of Animal
I used copy-paste to get your code, as you can see. Notice that after compiling there are three .class files in addition to your .java files.
«Cannot Find Symbol» compile error
My coding experience has only gone back a few years, so this question should be easy enough to answer. I have written two interfaces: Class and Game. Interface CLASS is supposed to extend interface GAME. Here are the two interface sources:
package Impl; public interface Game < //METHODS AND VARS >package Impl; public interface Class extends Game < //METHODS AND VARS >
class.java:4: cannot find symbol symbol: class Game public interface Class extends Game ^
My Game class is compiled and the class file is in the same directory as both java files. I have not been able to find a solution. Does anyone have any ideas?
2 Answers 2
Class names are case sensitive. It is possible that you have created an interface called game , but you refer to it in your Class interface declaration as Game , which the compiler cannot find.
However, there is another chance that you are compiling from within your Impl package. To do this, you will need to reference your classpath such that the compiler can find classes from the base of the package structure. You can add a -classpath .. arg to your javac before the class name:
javac -classpath .. Class.java
Alternatively, you can do what is more common, compiling from the root of your package structure. To do so, you will need to specify the path to your Class file:
you can always add a -classpath . to be clear.
Java error: cannot find symbol, and can’t figure out why
What appears doesn’t works: If I write: «InvoerVakhandler extends boven» then the error disapears but then I get an endless loop and in the end the program crashes and says stack overflow error. If I try to change the «>» symbol from class boven and place it in the end of the text then I even get more error messages. Question: What do I have to change in my code to make it work? Note: I’m new to Java and I know there are more posts like this one but I just can’t apply them to my code with my current limited understanding of Java. If someone wants to know: I’m using JCreator. What i’m trying to make: What I’m trying to make is fairly simple. 1) Fill in a name in the JTextField, press enter and the name should appear in the JTextArea. After the name is in the JTextArea the JTextField becomes empty so you can fill another name and so on there should appear a list of names in JTextArea. (this is what I’m now trying to make) 2) Push the button kiesWin to make the program choose a random person from the list. 3) Push the button resetL to reset the program so I can make a new list to choose a random winner from it. Part of the code where the error appears: (from the class InvoerVakHandler)
String invoer = invoervak1.getText();
With my limited knowledge of java the problem could be litterly anywhere so in case I will post the whole code. Whole code:
import javax.swing.*; import javax.swing.border.*; import java.awt.*; import java.awt.event.*; import java.util.*; // Main method to make the frame public class Loterij3 extends JFrame < public static void main( String args[] ) < JFrame frame = new Loterij3(); frame.setExtendedState( frame.MAXIMIZED_BOTH ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.setTitle( "Klanten Register" ); frame.setContentPane( new Paneel() ); frame.setVisible( true ); >> class Paneel extends JPanel < private boven Boven; JTextArea textvak1; JTextField textvak2; OnthoudNaam onthoudNaam = new OnthoudNaam(); public Paneel() < setLayout( new BorderLayout() ); // using border Layout. setBackground( Color.LIGHT_GRAY ); textvak1 = new JTextArea(); add( new JScrollPane( textvak1 ) ); textvak1.setBackground( Color.WHITE ); textvak2 = new JTextField(); textvak2.setHorizontalAlignment(JTextField.CENTER); textvak2.setEditable( false ); Boven = new boven(); add( Boven, BorderLayout.NORTH ); add( textvak1, BorderLayout.CENTER ); add( textvak2, BorderLayout.SOUTH ); >public class boven extends Paneel < JButton kiesWin, resetL; JLabel label1; JTextField invoervak1; public boven() < setBackground( Color.LIGHT_GRAY ); setLayout( new GridLayout( 1, 4, 100, 5 ) ); // using GridLayout. Border border = BorderFactory.createEmptyBorder( 10, 10, 10, 10 ); setBorder( border ); kiesWin = new JButton("Kies een Winnaar!"); kiesWin.addActionListener( new kies() ); resetL = new JButton("Reset alles"); resetL.addActionListener( new reset() ); label1 = new JLabel("Voer Persoon in en druk op enter: ", JLabel.RIGHT); invoervak1 = new JTextField( 20 ); invoervak1.addActionListener( new InvoerVakHandler() ); add( label1 ); add( invoervak1 ); add( kiesWin ); add( resetL ); >> // de naam class naam < private String ingevoerdNaam; public naam( String ingevoerdNaam) < this.ingevoerdNaam = ingevoerdNaam; >public String getIngevoerdNaam() < return ingevoerdNaam; >> // Arraylist class OnthoudNaam extends JPanel < private ArrayListlijst; public OnthoudNaam() < lijst = new ArrayList(); > public void voegNaamToe(naam x ) < lijst.add(x); >public String toString() < StringBuffer buffer = new StringBuffer(); for(naam x : lijst ) < buffer.append( x ); buffer.append( "\n" ); >return buffer.toString(); > > // this is the part where the code goes wrong public class InvoerVakHandler implements ActionListener < public void actionPerformed( ActionEvent e ) < String invoer = invoervak1.getText(); naam naam = new naam( invoer ); onthoudNaam.voegNaamToe( naam ); textvak1.setText( onthoudNaam.toString() ); >> // kies class kies implements ActionListener < public void actionPerformed( ActionEvent e ) < >> // reset class reset implements ActionListener < public void actionPerformed( ActionEvent e ) < >> >
extending class: cannot find symbol ?
posted 12 years ago
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.
Any help is soo much appreciated.
Saloon Keeper
posted 12 years ago
Hi Kiley, welcome to CodeRanch!
posted 12 years ago
posted 12 years ago
javac Unicycle.java yields the error.
posted 12 years ago
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!
posted 12 years ago
posted 12 years ago
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?
posted 12 years ago
javac has an option «-cp»/»-classpath»: «Specify where to find user class files and annotation processors». (javac -help)
Java Cowboy
posted 12 years ago
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 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 -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.