NoClassDefFoundError: opennlp/tools/chunker/ChunkerModel

NoClassDefFoundError: opennlp/tools/chunker/ChunkerModel

尝试 opennlp 分块时出现此错误:

NoClassDefFoundError: opennlp/tools/chunker/ChunkerModel

基本代码如下:

import java.io.*;
import opennlp.tools.chunker.*;

public class test{
        public static void main(String[] args) throws IOException{  
                ChunkerModel model = null;
                InputStream modelIn = new FileInputStream("en-parser-chunking.bin");
                model = new ChunkerModel(modelIn);
        }
}

我在这里没有看到任何特定于 NLP 的原因,所以只需检查 tutorials 关于 NoClassDefFoundError,例如:

Verify that all required Java classes are included in the application’s classpath. The most common mistake is not to include all the necessary classes, before starting to execute a Java application that has dependencies on some external libraries.

The classpath of the application is correct, but the Classpath environment variable is overridden before the application’s execution."

related question.

特别是,检查您的类路径中是否有合适的(且只有一个)版本的 opennlp jar。

*导入包的所有内容(通过使用通配符)不是一个好的风格 - 相反,使用 IDE 的支持:例如Eclipse 中的 ctrl+shift+o(IDEA 中的 ctrl+alt+o)自动解析所有需要的导入。