Java导入子子目录

时间:2014-10-17 20:26:24

标签: java

我的目录结构如下所示:

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

导入类文件的方式是错误的吗?

2 个答案:

答案 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的兄弟。