Java:打包类文件

时间:2014-01-30 14:13:57

标签: java java-package

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

我有以下3个java文件:Node.javaInvertedFile.javaPostings.java,所有这些文件都位于Mac OS X上的/Users/tracking/Desktop/implementation/mytree目录中。

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。我总是得到同样的错误。谁能帮我解决这个问题?提前谢谢。

2 个答案:

答案 0 :(得分:2)

Java希望在名为mytree的目录中找到源文件。从路径/Users/tracking/Desktop/implementation

编译源代码
javac mytree/*.java 

答案 1 :(得分:0)

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

javac -cp . Postings.java

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