“找不到符号”错误 - 即使是一个简单得可笑的例子
所以我花了几个小时试图解决这个问题。我搜索过互联网,搜索过 StackOverflow,问过一些同事(我是一名实习生),老实说没有人能告诉我发生了什么事!我整理了一个非常非常简单的示例来向您展示我在做什么(即使使用简单的示例我也会收到错误)
我有两个 .java
文件。一个是Test.java
,另一个是testClass.java
。
//testClass.java
package test;
public class testClass {
private int someMember=0;
public testClass(){
//kill me now
}
}
然后我有我的 Test.java 文件,其中包含我的主要方法。 (尽管在我真正的问题中,我没有一个 main 方法 - 它是一个带有 doGet() 方法的 servlet),
//Test.java
package test;
public class Test {
public static void main(String[] args) {
testClass myTest = new testClass();
}
}
我正在使用以下内容进行编译(从 Windows 命令行,使用我保存 . java 文件):
..java bin location..\javac testClass.java
这工作得很好,我在当前目录中得到一个 testClass.class 文件。然后,我尝试使用以下内容编译 Test.java 文件(再次在工作目录中):
..java bin location..\javac -classpath . Test.java
这会导致以下错误:
Test.java:6: cannot find symbol
symbol : class testClass
location : class test.testClass
testClass myTest = new testClass();
你能帮助兄弟吗? :(
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? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的类位于包中,Java 将查找假定该包结构的类 - 但 javac 不会为您构建该结构,除非您告诉它;它通常会将类文件与 Java 文件放在一起。
选项:
test
目录下,编译test\Test.java
和test\testClass.java
-d .
编译时,强制 javac 构建包结构。使用 IDE(Eclipse、IntelliJ 等)往往会鼓励甚至强制您将文件放在正确的目录中,并且通常也会使构建代码变得更容易。
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:
test
directory, and compiletest\Test.java
andtest\testClass.java
-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.
我完全按照你的做法
然后编译
然后运行它。这一切都奏效了。所以这告诉我问题出在你的编译方式上。您是否尝试过将两个类一起编译?
编辑 - 正如 Jon Skeet 所说,我确实将所有文件放在
test/
目录中。也许这就是不同之处。I did exactly what you did
Then compiled
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?
Edit - I did put all my files in
test/
dir as Jon Skeet has said. Maybe that's what is different.最简单的修复:创建一个目录
test
并将您的.java
放入其中,添加包含您的test-文件夹到类路径中。如果您不知道如何执行此操作,只需将
test
文件夹放入 java 文件夹的子文件夹lib
中(例如 c:\prog\javasdk\lib)。只需使用javac Test.java
进行编译(testClass 将自动编译),然后在任何地方使用java test.Test
运行它。Easiest fix: Create a directory
test
and place your.java
's in there, add the folder that contains yourtest
-folder into the classpath. If you dont know how to do that just place yourtest
-folder in your java-folder's subfolderlib
(e.g. c:\prog\javasdk\lib). Just compile withjavac Test.java
(testClass will compile automatically), run it withjava test.Test
from anywhere.