Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find class in same package

Tags:

I am trying to compile Board.java, which is in the same package (and directory) as Hexagon.java, but I get this error:

Board.java:12: cannot find symbol symbol  : class Hexagon location: class oadams_atroche.Board     private Hexagon[][] tiles; 

The first few lines of Board.java:

package oadams_atroche;  import java.util.LinkedList; import java.util.Queue; import java.io.PrintStream;  import p323.hex.*;  public class Board implements Piece{ >---//Fields >---private int n; >---private Hexagon[][] tiles; 

The first few lines of Hexagon.java:

package oadams_atroche;  import p323.hex.*;  public class Hexagon implements Piece{ 

I just cannot see what I am doing wrong. Any ideas?

Thanks

like image 231
oadams Avatar asked Sep 20 '10 06:09

oadams


People also ask

Can we have multiple Java classes with the same name in the same package?

A Java file contains only one public class with a particular name. If you create another class with same name it will be a duplicate class. Still if you try to create such class then the compiler will generate a compile time error.

How does compiler locate package?

The compiler relies on the names of source files to find and compile dependent classes. It's possible (and common) to put more than one class definition into a single file, but there are some restrictions we'll discuss shortly. A class is declared to belong to a particular package with the package statement.

Can a class be in multiple packages Java?

No, it cannot be.

Can I import same package class twice in Java?

Yes, you can import a class twice in Java, it doesn't create any issues but, irrespective of the number of times you import, JVM loads the class only once.


1 Answers

I'm quite sure you're compiling from within the wrong directory. You should compile from the source root-directory, and not from within the oadams_atroches directory.

Have a look at this bash-session:

aioobe@r60:~/tmp/hex/oadams_atroche$ ls Board.java  Hexagon.java aioobe@r60:~/tmp/hex/oadams_atroche$ javac Board.java  Board.java:12: cannot find symbol symbol  : class Hexagon location: class oadams_atroche.Board     private Hexagon[][] tiles;             ^ 1 error 

While if I go up one directory...

aioobe@r60:~/tmp/hex/oadams_atroche$ cd .. 

... and compile:

aioobe@r60:~/tmp/hex$ javac oadams_atroche/Board.java  aioobe@r60:~/tmp/hex$  
like image 82
aioobe Avatar answered Sep 22 '22 15:09

aioobe