Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot find symbol" error - even on a ridiculously simple example

So I've been trying to solve this problem for a matter of hours now. I've scoured the internet, I've scoured StackOverflow, I've asked some co-workers (I'm an intern) and honestly no one can tell me what is going on! I put together a really really simple example to show you what I'm doing (and I get the error even with the simple example)

I have two .java files. One is Test.java the other is testClass.java.

//testClass.java

package test;

public class testClass {
    private int someMember=0;

    public testClass(){
        //kill me now
    }

}

Then I have my Test.java file which contains my main method. (although in my real problemIi dont have a main method - its a servlet with a doGet() method)

//Test.java
package test;

public class Test {

    public static void main(String[] args) {
        testClass myTest = new testClass();
    }
}

I'm compiling with the following (from windows command line, with current directory where I saved my .java files):

..java bin location..\javac testClass.java

This works absolutely fine and I get a testClass.class file in the current directory. I, then, try to compile the Test.java file with the following (again within the working directory):

..java bin location..\javac -classpath . Test.java

This results in the following error:

Test.java:6: cannot find symbol
symbol : class testClass
location : class test.testClass
   testClass myTest = new testClass();

Can you please help a brother out? :(

like image 863
Dave Anderson Avatar asked Jul 21 '11 14:07

Dave Anderson


1 Answers

Your classes are in a package, and Java will look for classes assuming that package structure - but javac won't build that structure for you unless you tell it to; it will normally put the class file alongside the Java file.

Options:

  • Put the source files in test directory, and compile test\Test.java and test\testClass.java
  • Specify -d . when you compile, to force javac to build a package structure.

Using an IDE (Eclipse, IntelliJ etc) tends to encourage or even force you to put the files in the right directory, and typically makes building code easier too.

like image 63
Jon Skeet Avatar answered Oct 09 '22 09:10

Jon Skeet