use*_*661 1 java eclipse itext
原谅我对Java导入的困惑 - 我来自Python背景.
我有一些使用itext库的代码:
public static void makeADoc (Person p, String outfile) throws DocumentException,FileNotFoundException{
Document document = new Document;
PdfWriter.getInstance(document, new FileOutputStream(outfile));
document.open();
document.add(new Paragraph("Hello " + p.getFirst() + ",\n" + "You've just won the lotto!"));
document.close();
}
Run Code Online (Sandbox Code Playgroud)
我已将适用的itext-pdf jar文件添加到项目的路径中.我已使用通配符import语句在此类的开头导入了整个库:
import com.itextpdf.*;
Run Code Online (Sandbox Code Playgroud)
然而,Eclipse仍然为Document对象和DocumentException以及FileNotFound异常对象提供了红色下划线错误.我可以选择从itextpdf导入Document类,但看起来我的通配符语句应该已经覆盖了.这是怎么回事?
在FileNotFoundException从没有itextpdf包装但软件包java.io.所以你也应该添加这个import语句.还要记住,使用这样的通配符导入语句有时被认为是不好的做法,因为它可能会混淆您的命名空间.
此外,使用通配符语句,可以导入包中的所有类com.itextpdf.但是,类DocumentException是在包中com.itextpdf.text而不是com.itextpdf,因此您还必须添加此import语句.请注意,在Java中,没有子包的概念,即使人类有时使用这种类比.所以com.itextpdf.text是一个完全不同的包com.itextpdf.