“Cannot find symbol” error - even on a ridiculousl

2020-02-12 11:39发布

问题:

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? :(

回答1:

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.



回答2:

I did exactly what you did

$ ls -la test/
drwxr-xr-x  6 amirraminfar  staff  204 Jul 21 10:24 .
drwxr-xr-x  3 amirraminfar  staff  102 Jul 21 10:23 ..
-rw-r--r--  1 amirraminfar  staff  148 Jul 21 10:24 Test.java
-rw-r--r--  1 amirraminfar  staff  140 Jul 21 10:24 testClass.java

Then compiled

$ javac test/*

Then ran it. And it all worked. So this tells me the problem is the way you are compiling. Have you tried compiling both classes together?

 $ java test.Test

Edit - I did put all my files in test/ dir as Jon Skeet has said. Maybe that's what is different.



回答3:

Easiest fix: Create a directory test and place your .java's in there, add the folder that contains your test-folder into the classpath. If you dont know how to do that just place your test-folder in your java-folder's subfolder lib (e.g. c:\prog\javasdk\lib). Just compile with javac Test.java (testClass will compile automatically), run it with java test.Test from anywhere.