Java 在同一个包中找不到类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3749200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:21:16  来源:igfitidea点击:

Cannot find class in same package

java

提问by oadams

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,它与 Hexagon.java 位于同一包(和目录)中,但出现此错误:

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

The first few lines of Board.java:

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:

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

谢谢

采纳答案by aioobe

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

我很确定你是从错误的目录中编译的。您应该从源根目录编译,而不是从 oadams_atroches 目录中编译。

Have a look at this bash-session:

看看这个 bash 会话:

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$ 

回答by Roger Garzon Nieto

It works for me:

这个对我有用:

cd SRC_DIRECTORY
javac  -cp . PACKAGE/CLASS.java

回答by crimson30

Not sure about different platforms, but using Netbeans on Windows, it's often easiest to just create a project.

不确定不同的平台,但在 Windows 上使用 Netbeans,创建项目通常是最简单的。

If you're trying to compile from command line:

如果您尝试从命令行编译:

javac -cp . *.java

javac -cp 。*.java