Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 testTest.java and testestClass.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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...