“找不到符号"错误 - 即使在一个可笑的简单例子中 [英] "Cannot find symbol" error - even on a ridiculously simple example

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

问题描述

所以我已经尝试解决这个问题几个小时了.我搜索了互联网,我搜索了 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 文件,其中包含我的主要方法.(虽然在我真正的问题中我没有主方法 - 它是一个带有 doGet() 方法的 servlet)

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 命令行,使用我保存 .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 将寻找假定该包结构的类 - 但 javac 不会构建该结构为你,除非你告诉它;它通常会将类文件放在 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目录下,编译testTest.javatest estClass.java
  • 编译时指定-d .,强制javac构建包结构.
  • Put the source files in test directory, and compile testTest.java and test estClass.java
  • Specify -d . when you compile, to force javac to build a package structure.

使用 IDE(Eclipse、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天全站免登陆