7 Replies Latest reply on Dec 17, 2009 4:00 PM by 843810

    javac = "cannot find symbol" when compiling class referencing other classes

    843810
      I have several Java files in a directory which is declared as a package at the start of the files

      package filemanager;

      This program works perfectly in an IDE, however I'm having problems with compiling in command line.

      I have set the classpath as I understand to be right for my computer to:
      C:\Program Files\Java\jdk1.6.0_02\bin

      I can compile a class that references no other classes in this package/directory.

      However files which reference other classes bring up such errors:
      javac AllFiles.java
      AllFiles.java:174: cannot find symbol
      symbol : variable Bob
      location : class filesmanager.AllFiles
      Bob.getItem(itemRef);

      Where Bob is an example of another class. Even though I can say, compile Bob on it's own as it doesnt have any such reference.

      I've tried:
      javac -cp "C:\Program Files\Java\jdk1.6.0_02\bin" AllFiles.java
      This also failed.

      Any ideas?

      Edited by: ajr87 on Feb 8, 2008 10:26 AM
        • 1. Re: javac = "cannot find symbol" when compiling class referencing other cla
          843810
          I am assuming that these files are in a directory called "filemanager" (if not, they need to be). Then you need to change to the directory that contains "filemanager" (so you are kind of at the root of the hierarchy). And then from there, run "javac filemanager/AllFiles.java", so that the path of the source file relative to where you are compiling it (filemanager/AllFiles.java) matches its fully-qualified name (filemanager.AllFiles).
          • 2. Re: javac = "cannot find symbol" when compiling class referencing other cla
            843810
            I've tried spoon_'s suggestion, but I'm still getting the error ("cannot find symbol"). Here is my code:

            File Hello/Fred.java:
            package Hello;
            
            public class Fred
            {
                public int age;
            
                public Fred()
                {
                    this.age = 1;
                }
            } // public class Fred
            File Hello/Hello.java:
            package Hello;
            
            public class Hello
            {
                public static void main(String args[])
                {
                    Fred fred = new Fred();
                    System.out.println("Fred is " + fred.age);
                }
            } // public class Hello
            And here is my attempt to compile Fred.java and Hello.java:
            C:/Users/levner/programs[219]javac -cp c:\users\levner\programs\hello Hello\Fred.java
            C:/Users/levner/programs[220]javac -cp c:\users\levner\programs\hello Hello\Hello.java
            Hello\Hello.java:14: cannot find symbol
            symbol  : class Fred
            location: class Hello.Hello
                    Fred fred = new Fred();
                    ^
            Hello\Hello.java:14: cannot find symbol
            symbol  : class Fred
            location: class Hello.Hello
                    Fred fred = new Fred();
                                    ^
            2 errors
            I am doing this work on a PC running Vista using javac 1.6.0_10. I've tried various combinations of upper and lower case names for the package and file names, and I always get the same errors.

            Thanks in advance for your help. David
            • 3. Re: javac = "cannot find symbol" when compiling class referencing other cla
              843810
              OK, I found the compile error. Here is the correct way to compile hello/Hello.java:
              C:/Users/levner/programs[233]javac -cp c:\Users\levner\programs hello\Hello.java
              Or, more simply, like this:
              C:/Users/levner/programs[242]javac -cp . hello\Hello.java
              Then to run it, I have to type this:
              C:/Users/levner/programs[249]java hello.Hello
              I can see why people use IDEs.
              • 4. Re: javac = "cannot find symbol" when compiling class referencing other cla
                EJP
                In other words your package is called 'Hello' but your directory is called 'hello'.

                Don't do that.

                It is customary for packages to be all lower-case.
                • 5. Re: javac = "cannot find symbol" when compiling class referencing other cla
                  843810
                  Thanks for the tip. I was copying the convention of a vendor whose code I am modifying--the vendor named its sample package TestJavaClient.

                  What should it have been? If the package is named testjavaclient, then the words all run together. Is test_java_client better?

                  By the way, I'm doing this work under Windows which doesn't distinguish between upper and lower case in file names. Although I try to name files and directories to be consistent with class and package names, the OS sometimes sabotages my efforts. I prefer Linux, but there are reasons why I am (for now) writing the code on a Windows platform.
                  • 6. Re: javac = "cannot find symbol" when compiling class referencing other cla
                    EJP
                    I'm doing this work under Windows which doesn't distinguish between upper and lower case in file names
                    But Java does. That's the point.

                    How you spell your package names is up to you. Whether they have to agree in case with the directory names isn't up to you, or Windows either. It is a rule of Java that they do.
                    • 7. Re: javac = "cannot find symbol" when compiling class referencing other cla
                      843810
                      Thank you! you reply helped me in solving a similar problem i was facing.

                      regards
                      aseem