Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
296 views
in Technique[技术] by (71.8m points)

java - "Cannot find symbol" for my own class

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 SettingsjoepMy DocumentsGCJsrccodejam2011Round0D>dir /B
Case.class
Case.java
EntryPoint.java

C:Documents and SettingsjoepMy DocumentsGCJsrccodejam2011Round0D>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 SettingsjoepMy DocumentsGCJsrccodejam2011Round0D>

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 SettingsjoepMy DocumentsGCJsrc>javac -cp . codejam2011/Round0/D/EntryPoint.java

codejam2011Round0DEntryPoint.java:16: cannot access codejam2011.Round0.D.Case

bad class file: .codejam2011Round0DCase.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 SettingsjoepMy DocumentsGCJsrc>

Update 2: It appears to be grabbing the Case.java file from a different package.

C:Documents and SettingsjoepMy DocumentsGCJsrc>javac -d ../classes codejam2011Round0D*.java

.codejam2011Round0DCase.java:4: duplicate class: codejam2011.Round0.C.Case
public class Case
       ^
codejam2011Round0DEntryPoint.java:16: cannot access codejam2011.Round0.D.Case

bad class file: .codejam2011Round0DCase.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 SettingsjoepMy DocumentsGCJsrc>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to compile from the package root, not from inside the package.

So, cd to the src folder and compile from there.

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...