简体   繁体   English

Eclipse JDT编译器表示方法未定义,但是Eclipse IDE并未定义

[英]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). 我正在使用一个名为iText的库(已使用JAR文件添加到项目中)。 Its API can be seen here: https://coderanch.com/how-to/javadoc/itext-2.1.7/com/lowagie/text/Image.html 可以在这里看到其API: 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: 在我的项目中,我有一个简单的Java文件,名为Worker.java ,它使用以下库:

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. 在上面的代码中,使用img.getHeight()函数检索了Image对象的高度。 This function is part of the com.lowagie.text.Rectangle class, which the Image class extends. 此函数是com.lowagie.text.Rectangle类的一部分,该类是Image类的扩展。

When compiling this code in Eclipse, the IDE quickly recognizes that the function comes from the Rectangle class and compiles without any errors. 在Eclipse中编译此代码时,IDE会快速识别出该函数来自Rectangle类,并且编译时没有任何错误。

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: 但是,如果我使用独立的ecj-4.4.jar文件通过批处理编译器( BatchCompiler.compile(...) )编译项目,则编译器将报告以下错误:

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? 如果是真正的错误,那么为什么Eclipse也不会报告它呢?

EDIT: There are two versions of this JAR in the classpath, which is why the error seems to be showing up. 编辑:在类路径中有这个JAR的两个版本,这就是为什么错误似乎会出现的原因。 Unfortunately, since it's a large project that multiple people are part of, I can't remove the duplicate JAR from the project. 不幸的是,由于这是一个包含多个人的大型项目,所以我无法从项目中删除重复的JAR。 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? 但是,在正确的JAR中找到该方法,Eclipse IDE似乎没有任何问题,那么为什么编译器会出现此问题?

While eclipse takes it's classpath settings from the corresponding project the batch compiler doesn't. eclipse从相应项目中获取类路径设置,而批处理编译器则没有。 You have to include your libraries into your classpath. 您必须将库包含到类路径中。

See the batch compiler's documentation here and have a look at the -cp option. 请参阅此处的批处理编译器文档,并查看-cp选项。 You should include the corresponding library there. 您应该在其中包括相应的库。

EDIT: Having the same jars in both classpaths does not suffice. 编辑:在两个类路径中具有相同的jars是不够的。 You also have to take the order into account. 您还必须考虑订单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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