0

I am using a library called iText (added to project using JAR file). Its API can be seen here: https://coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/Image.html

In my project, I have a simple Java file, called Worker.java, that uses this library:

import com.lowagie.text.Image;
public class Worker {
    public void createDetails() {
        Image img;
        try {
            img = Image.getInstance("...");
            float h = img.getHeight();
            float w = img.getWidth();
            ...
        } catch (Exception e) {...}
    }
}

In the above code, the height of the Image object is being retrieved using the img.getHeight() function. This function is part of the com.lowagie.text.Rectangle class, which the Image class extends.

When compiling this code in Eclipse, the IDE quickly recognizes that the function comes from the Rectangle class and compiles without any errors.

However, if I use the standalone ecj-4.4.jar file to compile the project with the Batch Compiler (BatchCompiler.compile(...)), the following error is reported by the compiler:

1. ERROR in C:\...\Worker.java (at line 7)
        float h = img.getHeight();
                      ^^^^^^^^^
The method getHeight() is undefined for the type Image
----------

I just can't figure out why this error is being thrown. If it's a genuine error, then why isn't Eclipse reporting it as well?

EDIT: There are two versions of this JAR in the classpath, which is why the error seems to be showing up. Unfortunately, since it's a large project that multiple people are part of, I can't remove the duplicate JAR from the project. However, Eclipse IDE doesn't seem to have any problem looking in the right JAR for the method, so why is the compiler having this issue?

DemCodeLines
  • 1,870
  • 8
  • 41
  • 60
  • As my answer already suggests: Not only is it important to have the same jars in both classpaths, it's alsothe order of the jars that is important. Usually the jars are searched in order and the first class definition found is the one loaded. – Björn Zurmaar Jul 14 '17 at 15:11
  • How is the Eclipse IDE able to find the method? – DemCodeLines Jul 14 '17 at 15:14
  • My guess is that there are two versions of the library, one with this method the other one without. Eclipse has most likely the version with that method first in its classpath while the batch compiler probably has the version with the missing method first. – Björn Zurmaar Jul 14 '17 at 15:19

1 Answers1

1

While eclipse takes it's classpath settings from the corresponding project the batch compiler doesn't. You have to include your libraries into your classpath.

See the batch compiler's documentation here and have a look at the -cp option. You should include the corresponding library there.

EDIT: Having the same jars in both classpaths does not suffice. You also have to take the order into account.

Björn Zurmaar
  • 826
  • 1
  • 9
  • 22
  • Thank you for your response. However, I am already using the `-classpath` option to give it the JAR – DemCodeLines Jul 13 '17 at 21:50
  • Care to show us the command line you're using? Maybe that's a hint into the right direction. – Björn Zurmaar Jul 13 '17 at 22:55
  • This wouldn't explain _that_ error message. If the compiler couldn't find the library class `Image`, it would complain about that. – Tom Jul 14 '17 at 06:07
  • That's right if it couldn't find that. But there is also the possibility of the two including different versions of the jar in question. – Björn Zurmaar Jul 14 '17 at 12:16