2011-05-13 145 views
7

我没有设置%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:

尝试(即使删除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> 

从我的包根(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> 
+0

您是否在EntryPoint中导入class Case? – Danish 2011-05-13 21:43:33

+0

@Danish:这是没有必要的,因为它在同一个包中。 – BalusC 2011-05-13 21:47:28

+0

你学习了错误信息吗? '重复类:codejam2011.Round0.C.Case'有C和D - 含糊不清。你必须排除一些导入,或者明确地指定哪个Case,codejam2011.Round0.C.Case或codejam2011.Round0.D.Case的意思。 – 2011-05-13 22:25:03

回答

18

您需要从软件包根目录进行编译,而不是从软件包内部进行编译。

因此,cdsrc文件夹并从那里编译。

javac -cp . codejam2011/Round0/D/EntryPoint.java 

更新:按你的新的问题,您需要重新编译Case.java相同的方式。显然,编译方式是相同的(从包内部)。

+0

当我将类路径设置为'.'并且使用包时,我会得到同样的确切错误 - 也许这就是为什么它不起作用?我的包是codejam2011.Round0.D – 2011-05-13 21:44:25

+0

我更新了答案。我没想到你是从包里面编译的,这不是要做的。 – BalusC 2011-05-13 21:46:12

0

你在错误的目录中进行编译。

location: class codejam2011.Round0.D.EntryPoint 

这告诉我,你的包是codejam2011.Round0.D(这是对公约(全部小写),但跑题了...

光盘codejam2011的父目录,这为src,是不是

javac codejam2011\Round0\D\EntryPoint.java 

可能做的伎俩

通常你有一个目录编译的类,如“斌”或“类”到那里产生的类,使用? - d (目的地):

javac -d ../classes codejam2011\Round0\D\EntryPoint.java 
+0

感谢关于小写包约定的提示。我会解决这个问题。至于编译,还是没有运气 – 2011-05-13 21:57:11

+0

什么是新错误? Case属于哪个包?它是否在同一个包中(容易)?没有包(:=匿名,不可能)。你有没有特别的案例 - 我不这么认为。试试'javac -d ../classes codejam2011 \ Round0 \ D \ *。java'。 – 2011-05-13 22:01:11

+0

Case和EntryPoint处于相同的包中。 (codejam2011.Round0.D) – 2011-05-13 22:04:38

1

如果问题还没有从包根目录编制(见其他答案)解决:

  • 确保所有的源文件包含有对应的名称类他们的文件名
  • 请确保所有源文件都包含与源文件层次结构中的位置相对应的包语句
  • 在编译之前删除所有的.class文件(这应该只需要一次,如果您检查编辑一切)。

因此,如果该文件是codejam2011\Round0\D\Case.java,它应该包含package codejam2011.Round0.D;作为第一声明,然后public class Case { ... }。另外,请确保没有包含此包和类声明的其他源文件。

从您的错误消息,它看起来像包装语句是package codejam2011.Round0.C;而不是(你也有一个类Case在真正的codejam2011.Round0.C包)。

相关问题