简体   繁体   中英

Eclipse JDT compiler says method is undefined, but Eclipse IDE doesn't

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?

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.

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