“找不到符号”为我自己的班级
我没有设置 %CLASSPATH%。据我了解,这应该不是问题,因为 Javac 将假定当前目录的类路径。
正如您在下面看到的,javac 无法找到我的 Case
类,即使它位于同一个目录中。对于为什么会发生这种情况有什么想法吗?当我使用 Eclipse 时,这段代码运行良好。
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>dir /B
Case.class
Case.java
EntryPoint.java
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>javac EntryPoint.java
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:24: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
cases.add(new Case(new Integer(count), line));
^
3 errors
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>
更新 1:
尝试从包根 (src) 进行编译后,我收到一个新错误(即使在删除 Case.class 文件之后)
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -cp . codejam2011/Round0/D/EntryPoint.java
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
1 error
C:\Documents and Settings\joep\My Documents\GCJ\src>
更新 2: 它似乎是从不同的包中获取 Case.java 文件。
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -d ../classes codejam2011\Round0\D\*.java
.\codejam2011\Round0\D\Case.java:4: duplicate class: codejam2011.Round0.C.Case
public class Case
^
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
2 errors
C:\Documents and Settings\joep\My Documents\GCJ\src>
I do not have a %CLASSPATH% set up. As I understand, this should not be a problem because Javac will assume a classpath of the current directory.
As you can see below, javac is unable to find my Case
class even though it's in the same exact directory. Any thoughts on why this is happening? This code works fine when I use Eclipse.
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>dir /B
Case.class
Case.java
EntryPoint.java
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>javac EntryPoint.java
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:16: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
ArrayList<Case> cases = new ArrayList<Case>();
^
EntryPoint.java:24: cannot find symbol
symbol : class Case
location: class codejam2011.Round0.D.EntryPoint
cases.add(new Case(new Integer(count), line));
^
3 errors
C:\Documents and Settings\joep\My Documents\GCJ\src\codejam2011\Round0\D>
Update 1:
After trying to compile from my package root (src), I get a new error (even after deleting the Case.class file)
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -cp . codejam2011/Round0/D/EntryPoint.java
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
1 error
C:\Documents and Settings\joep\My Documents\GCJ\src>
Update 2:
It appears to be grabbing the Case.java file from a different package.
C:\Documents and Settings\joep\My Documents\GCJ\src>javac -d ../classes codejam2011\Round0\D\*.java
.\codejam2011\Round0\D\Case.java:4: duplicate class: codejam2011.Round0.C.Case
public class Case
^
codejam2011\Round0\D\EntryPoint.java:16: cannot access codejam2011.Round0.D.Case
bad class file: .\codejam2011\Round0\D\Case.java
file does not contain class codejam2011.Round0.D.Case
Please remove or make sure it appears in the correct subdirectory of the classpath.
ArrayList<Case> cases = new ArrayList<Case>();
^
2 errors
C:\Documents and Settings\joep\My Documents\GCJ\src>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要从包根目录进行编译,而不是从包内部进行编译。
因此,
cd
到src
文件夹并从那里进行编译。更新:根据您的新问题,您需要以相同的方式重新编译
Case.java
。它显然是以相同的错误方式编译的(从包内部)。You need to compile from the package root, not from inside the package.
So,
cd
to thesrc
folder and compile from there.Update: as per your new problem, you need to recompile
Case.java
the same way. It was apparently compiled the same wrong way (from inside the package).如果从包根目录编译还没有解决问题(请参阅其他答案):
因此,如果文件是
codejam2011\Round0\D\Case.java
,则它应包含package codejam2011.Round0.D;
作为第一个声明,然后是公共类案例{...}
。另外,请确保没有其他源文件包含此包和类声明。从您的错误消息来看,包语句似乎是
package codejam2011.Round0.C;
(并且您在真实的codejam2011.Case 中也有一个类
包)。Case
)。 Round0.CIf the problem is not yet solved by compiling from the package root directory (see the other answers):
Thus, if the file is
codejam2011\Round0\D\Case.java
, it should containpackage codejam2011.Round0.D;
as the first declaration, and thenpublic class Case { ... }
. Also, make sure there is no other source file containing this package and class declaration.From your error message, it looks like the package statement is
package codejam2011.Round0.C;
instead (and you also have a classCase
in the realcodejam2011.Round0.C
package).您处于错误的编译目录中。
这告诉我,你的包是 codejam2011.Round0.D (这违反了惯例(全部小写),但离题...
cd 到 codejam2011 的父目录,即 src,不是吗?
可能会这样做 通常,
您有一个用于编译类的目录,例如“bin”或“classes”,要在其中生成类,请使用-d(目标):
You are in the wrong directory for compiling.
That tells me, that your package is codejam2011.Round0.D (which is against the convention (all lowercase) but beside the point ...
cd to the parent dir of codejam2011, which is src, isn't it?
might do the trick.
Often you have a directory for compiled classes, like 'bin' or 'classes'. To produce the classes there, use -d (destination):
我有类似的问题,它可能不适用于所有情况,但我所做的是删除 .gradle、build 和 out 文件夹并再次重建程序。
I have similar issue, it might not apply to all cases, but what I have done is remove .gradle, build and out folder and rebuild the program again.