Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java import statement with * not picking up class

Tags:

java

(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?

like image 310
Sabuncu Avatar asked Dec 26 '14 17:12

Sabuncu


1 Answers

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).

like image 144
Sotirios Delimanolis Avatar answered Oct 21 '22 05:10

Sotirios Delimanolis