"Cannot find symbol" for my own class
Asked Answered
B

4

9

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>
Bul answered 13/5, 2011 at 21:38 Comment(5)
Did you import class Case in EntryPoint?Wet
@Danish: That's not necessary since it's in the same package.Burden
Did you study the error messages? duplicate class: codejam2011.Round0.C.Case There is C and D - an ambiguity. You have to exclude some imports, or specify explictly which Case, codejam2011.Round0.C.Case or codejam2011.Round0.D.Case you mean.Florettaflorette
@user They're in different packages though. Why would the compiler get confused and try to use one from a different package?Bul
I don't have an overview of your source dir(s), your target dir(s), the current dir, environment variables and the history of your actions. You should start deleting all old classes.Florettaflorette
B
24

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

Burden answered 13/5, 2011 at 21:41 Comment(2)
I get the same exact error when I set the classpath to . and I am using packages -- maybe that's why it's not working? My package is codejam2011.Round0.DBul
I updated the answer. I didn't expect that you were compiling from inside the package, that's not how it's to be done.Burden
G
2

If the problem is not yet solved by compiling from the package root directory (see the other answers):

  • make sure all the source files contain classes with names corresponding to their file name
  • make sure all the source files contain a package statement corresponding to their position in the source file hierarchy
  • delete all your .class files before compiling (this should only be necessary once, if you checked everything else).

Thus, if the file is codejam2011\Round0\D\Case.java, it should contain package codejam2011.Round0.D; as the first declaration, and then public 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 class Case in the real codejam2011.Round0.C package).

Gunshot answered 15/5, 2011 at 13:43 Comment(1)
Thank you so much for your answer and explaining everything one by one! Finally, this solved the issue!Lornalorne
F
0

You are in the wrong directory for compiling.

location: class codejam2011.Round0.D.EntryPoint

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?

javac codejam2011\Round0\D\EntryPoint.java

might do the trick.

Often you have a directory for compiled classes, like 'bin' or 'classes'. To produce the classes there, use -d (destination):

javac -d ../classes codejam2011\Round0\D\EntryPoint.java
Florettaflorette answered 13/5, 2011 at 21:49 Comment(4)
Thanks for the tip about lowercase package convention. I'll fix that. As for the compiling, still no luckBul
What is the new error? What package does Case belong to? Is it in the same package (easy)? In no package (:=anonymous, not possible). Do you have a special import for Case - I don't think so. Try javac -d ../classes codejam2011\Round0\D\*.java.Florettaflorette
Case and EntryPoint are in the same package. (codejam2011.Round0.D)Bul
And did you perform javac -d ../classes codejam2011\Round0\D\*.java? Errormessage?Florettaflorette
M
0

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.

Michaud answered 11/4, 2018 at 20:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.