我已经学习了Java软件包。不幸的是,我不明白为什么编译这些.java文件会导致编译错误。
我有以下3个Java文件:Node.java
,InvertedFile.java
和Postings.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
。我总是遇到相同的错误。谁能帮我解决这个问题?预先感谢您。
Java希望在名为mytree
的目录中找到源文件。从路径/Users/tracking/Desktop/implementation
javac mytree/*.java
从/用户/跟踪/桌面/实现以下命令
javac -cp . Postings.java
node.class应该在与软件包匹配的子目录中。