“无法找到符号”错误 - 甚至一个可笑的简单例子 [英] "Cannot find symbol" error - even on a ridiculously simple example

查看:259
本文介绍了“无法找到符号”错误 - 甚至一个可笑的简单例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在试图解决这个问题大约几个小时。我已经淘汰了互联网,我已经淘汰了StackOverflow,我已经问了一些同事(我是一个实习生),老实说,没有人可以告诉我发生了什么!我把一个非常简单的例子告诉你我在做什么(和简单的例子,我得到的错误)

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)

我有两个 .java 文件。一个是 Test.java ,另一个是 testClass.java

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
    }

}

然后我有我的Test.java文件,其中包含我的main方法。 (虽然在我真正的问题,我没有一个main方法 - 它的一个servlet与 doGet()方法)

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();
    }
}

我正在编译以下(从Windows命令line,与保存我的.java文件的当前目录):

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

这个工作非常好,我在当前目录下得到一个testClass.class文件。然后,我尝试使用以下(再次在工作目录中)编译Test.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

这会导致以下错误:

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

你能帮助一个兄弟吗? :(

Can you please help a brother out? :(

推荐答案

你的类是在一个包中,Java会寻找类假设包结构

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.

选项:


  • 将源文件放在 test 目录中,并编译 test\\ \\Test.java test\testClass.java

  • 指定

  • 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.

在编译时强制javac构建一个包结构。 ,IntelliJ等)往往鼓励或甚至强迫你把文件放在正确的目录,通常使建筑代码更容易。

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.

这篇关于“无法找到符号”错误 - 甚至一个可笑的简单例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆