简体   繁体   中英

Java import statement with * not picking up class

(Disclaimer: I am new to Java and also, I have read the related SO question .)

I have the following code:

import org.apache.pdfbox.pdmodel.*;
...
PDFont font = PDType1Font.HELVETICA_BOLD;

But the PDFont class is not recognized in Eclipse.

When I add the following:

import org.apache.pdfbox.pdmodel.font.PDFont;

The PDFont class is picked up.

Given that the PDFont class is located under the hierarchy specified in the first import statement ending with the asterisk, why is a specific import statement necessary?

Also, is there a way to search for the location of a class in a library if one doesn't have the documentation handy?

The on demand import declarations are not recursive

A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.

Your PDFont type is in package

org.apache.pdfbox.pdmodel.font

but you've tried to import

 import org.apache.pdfbox.pdmodel.*;

the package org.apache.pdfbox.pdmodel does not contain a type named PDFont .

So, alternatively, you could have used

 import org.apache.pdfbox.pdmodel.font.*;

Also, is there a way to search for the location of a class in a library if one doesn't have the documentation handy?

If the library is published as a .jar , you can unzip it and search through it.

Most IDEs typically have a feature to search types by their simple name. For example, in Eclipse, you can use CTRL (cmd) + SHIFT + T and type the simple or fully qualified name to search for (if it's on the classpath).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM