Forgive my confusion with Java imports — I come from a Python background.
I have some code that makes use of the itext library:
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();
}
I have added the applicable itext-pdf jar files to the project’s path. I have imported the entire library at the beginning of this class with a wildcard import statement:
import com.itextpdf.*;
Yet Eclipse is still giving me red underlined errors for Document objects and DocumentException and FileNotFound Exception objects. I am given the option to import the Document class from itextpdf, but it seems like my wildcard statement should have covered that. What’s going on?
The
FileNotFoundExceptionis not from theitextpdfpackage but from the packagejava.io. So you should also add this import statement. Keep also in mind that using such wildcards import statements is sometimes considered bad practice because it could clutter your namespace.Additionally, with your wildcard statement, you importing all the classes that are in the package
com.itextpdf. However, the classDocumentExceptionis in the packagecom.itextpdf.textand notcom.itextpdf, so you would also have to add this import statement. Note that in Java there is no concept of sub packages, even though humans sometimes use this analogy. Socom.itextpdf.textis a completely different package thancom.itextpdf.