我的目录结构如下所示:
folder
└───subfolder
└───subsubfolder
我在folder
中有一个Main.java,Main.java
使用subsubfolder
内的类。
我是这样做的:
import subfolder.*;
import subfolder.subsubfolder.*;
但是,当我执行javac Main.java
$ javac -g Main.java
Main.java:23: error: cannot access Node
Node root = new Node();
^
bad class file: ./subfolder/subsubfolder/Node.class
class file contains wrong class: subsubfolder.Node
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
导入类文件的方式是错误的吗?
答案 0 :(得分:6)
它说
package subfolder
Node
的包裹声明应该说
package subfolder.subsubfolder;
为清晰起见提供一个例子:
folder/
Your source root (typically called 'src')
folder/Main.java
class Main { ... } (no package declaration)
folder/subfolder
folder/subfolder/subsubfolder/Node.java
package subfolder.subsubfolder;
public class Node { ... }
如果你的Main
确实存在于一个软件包中(例如,如果你的情况类似于src/folder/Main.java
)那么你应该不做
cd src/folder
javac Main.java
你应该做
cd src
javac folder/Main.java
答案 1 :(得分:1)
您的Node类声明它属于包subsubfolder
,但 属于包subfolder.subsubfolder
。或者,您可以将目录subfolder/subsubfolder
移动为目录subfolder
的兄弟。