3

I'm working through the example here:

http://www.vogella.com/articles/JavaPDF/article.html

In my file, I've got:

package com.mycompanyname.mydirectory;

import com.mycompanyname.OneOfMyClasses;
import com.itextpdf.text.Document;
...

 public class MyClass {
     ...
 }

Everything is working fine. What I don't understand is that since I just copied the import statement directly from the link above for the iText portion -- why does com.itextpdf.text.Document work?

I mean, if I look in directory com.mycompanyname I can see OneOfMyClasses.java there.

But in the com directly, there is no itextpdf directory (although maybe my user doesn't have permission to see it(?)).

Hoping someone can help me understand what I'm missing here. Doesn't the import point to a specific directory that I should be able to see the class? Is there a different com directory somewhere that iText is using, and com.itextpdf.text points to there? (if so, where's the directory located)?

I installed the jar file for iText in the lib folder as per usual, and made sure it was included in the classpath.

0

4 Answers 4

7

Those classes are inside a JAR file that is added to the classpath:

Create a new Java project "de.vogella.itext.write" with the package "de.vogella.itext.write". Create a folder "lib" and put the iText library (jar file) into this folder. Add the jar to your classpath.

import statements will look inside whatever directory trees are in the classpath, which includes the current directory at compilation time (tipically the src/ directory in your project) as well as any directory specified through environment variable or JVM startup parameter. See this about the classpath.

EDIT

You do need the imports whenever you use classes across packages. Every public class/interface you define is in a package. If whatever you are referencing belongs to another package, you need to import it.

JARs are zip files that contain directories and files inside. It's the same as plain directories and files, only packed.

3
  • Yes, I did all that. Is my import statement even needed then, or is there a more logical way to write it?
    – ggkmath
    Jun 25, 2013 at 23:56
  • 1
    @ggkmath, unless your class is in the same package as the class it uses then you must use the import statement. But you can use a wildcard if you import lots from the same package, e.g. import com.itextpdf.text.*;
    – earcam
    Jun 25, 2013 at 23:59
  • 1
    @ggkmath wildcard imports are easy to use but also to misuse, so they are discouraged (they may result in unintentional cross-package name conflicts which may or may not be detected by the compiler)
    – LexLythius
    Jun 26, 2013 at 0:03
1

It comes from the iText dependency (jar) you added in an earlier step.

1

Not necessarily - you could also import from libraries, etc.

In fact, Java will try to search through the classpath. Here is some helpful documentation.

2
  • OK, assuming Java found it in the classpath, which it should, can I clean up the code somehow as not to mislead myself?
    – ggkmath
    Jun 25, 2013 at 23:55
  • Java will always need that line to find the class, so there's really not much you could do. If you are using eclipse, then any unused import should give you a warning, which might help you cleanup the code.
    – zw324
    Jun 25, 2013 at 23:57
1

That class is most probably imported in a JAR library. Inside such JAR file, the class files are kept in exact package/folder structure as you use when importing them.

4
  • Then, is my import statement for com.itextpdf.text.Document redundant or unnecessary? How to clean it up so it's not confusing?
    – ggkmath
    Jun 25, 2013 at 23:55
  • 1
    @ggkmath, inside such JAR file, the classes will be present to follow the same directory structure. No it is not a redundancy.
    – Starx
    Jun 25, 2013 at 23:56
  • OK, I think I see now. The com.itextpdf.text refers to the directory structure in the jar file and so there's nothing I can do to change it, and even more important, it is required to be included as such. Right?
    – ggkmath
    Jun 25, 2013 at 23:58
  • @ggkmath, Exactly, You got it.
    – Starx
    Jun 26, 2013 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.