1

我学过Java包。不幸的是,我不明白为什么编译这些 .java 文件会导致编译错误。

我有以下 3 个 java 文件:Node.javaInvertedFile.javaPostings.java,它们都在/Users/tracking/Desktop/implementation/mytreeMac OS X的目录中。

package     mytree;
import      java.util.*;

// Node.java

public class Node 
{
    private int             label;          // unique integer identifier of this tree.
    private Node            parent; 
    private List<Integer>   leaves;         // the leaf value that this tree contains.
    private List<Node>      children;       // internal nodes.  


    public Node(int uniqueID)       
    {
        this.parent     =   null;
        this.label      =   uniqueID;
        this.children   =   new ArrayList<Node>();
        this.leaves     =   new ArrayList<Integer>();
    }

    public void add_leafnode(int data)
    {
        leaves.add(data);
    }

    public void add_internalnode(Node ChildNode)
    {
        children.add(ChildNode);
    }

    // print all leaves, print all children node identifier methods.

    public void print_all_leaves()
    {
        Iterator iter = leaves.iterator();
        while( iter.hasNext() )
        {
            Integer element = (Integer)iter.next();
            System.out.println(element);
        }
    }

    public List<Node> Get_children()
    {
        return children;
    }

}

package     mytree;
import      java.util.*;

// Postings.java

class Postings
{
    private     int             parentID;
    private     List<Node>      siblings;       // internal nodes.

    public Postings(int parentID, List<Node> siblings)
    {
        this.parentID   =   parentID;
        this.siblings   =   siblings;
    }

    public void print()
    {
        System.out.println(parentID);

        Iterator iter = siblings.iterator();

        while( iter.hasNext() )
        {
            Integer element = (Integer)iter.next();
            System.out.println(element);
        }
    }
}

package     mytree;
import      java.util.*;

// InvertedFile.java

class InvertedFile
{
    private HashMap IF;

    public InvertedFile()
    {
        IF  =   new HashMap();
    }

    public void Add_Posting(int InvertedList, Postings posting)
    {
        IF.put(InvertedList, posting);
    }

}

问题是java编译器不知道Node.java实际在哪里~/Desktop/implementation/mytree。它报告给我的编译错误没有任何帮助。

Tracking:mytree tracking$ pwd
/Users/tracking/Desktop/implementation/mytree
Tracking:mytree tracking$ javac Node.java 
Tracking:mytree tracking$ javac Postings.java 
Postings.java:7: cannot find symbol
symbol  : class Node
location: class mytree.Postings
    private     List<Node>      siblings;       // internal nodes.
                     ^
Postings.java:9: cannot find symbol
symbol  : class Node
location: class mytree.Postings
    public Postings(int parentID, List<Node> siblings)
                                       ^
2 errors

另外,我尝试使用javac -d . Postings.java. 我总是得到同样的错误。谁能帮我解决这个问题?先感谢您。

4

2 回答 2

2

Java 期望在名为mytree. 从路径编译源码/Users/tracking/Desktop/implementation

javac mytree/*.java 
于 2014-01-30T14:16:39.847 回答
0

使用以下命令从 /Users/tracking/Desktop/implementation 编译

javac -cp . Postings.java

Node.class 应该位于与包匹配的子目录中。

于 2014-01-30T14:21:37.190 回答