如何解决java.lang.NoClassDefFoundError在下面的代码?

问题描述 投票:1回答:1

我是新来的NLP和JAVA。最近,我开始对语言检测工作,我得到了How to detect language of user entered text?代码。我使用NetBeans 8.2,并在其中复制下面的代码:

package landslip_langdetect;

import java.util.ArrayList;
import com.cybozu.labs.langdetect.Detector;
import com.cybozu.labs.langdetect.DetectorFactory;
import com.cybozu.labs.langdetect.LangDetectException;
import com.cybozu.labs.langdetect.Language;

public class LanguageCodeDetection {
    public void init(String profileDirectory) throws LangDetectException {
        DetectorFactory.loadProfile(profileDirectory);
}

public String detect(String text) throws LangDetectException {
    Detector detector = DetectorFactory.create();
    detector.append(text);
    return detector.detect();
}

public ArrayList<Language> detectLangs(String text) throws LangDetectException {
    Detector detector = DetectorFactory.create();
    detector.append(text);
    return detector.getProbabilities();
}

public static void main(String args[]) {
    try {
        LanguageCodeDetection ld = new LanguageCodeDetection();
        String profileDirectory = "C:\\Users\\user\\Documents\\NetBeansProjects\\LangDetect\\profiles\\";
        ld.init(profileDirectory);
        String text = "Кремль россий";
        System.out.println(ld.detectLangs(text));
        System.out.println(ld.detect(text));
    } catch (LangDetectException e) {
    }
}

}

“配置文件”文件夹,我只是从GitHub(https://github.com/shuyo/language-detection)下载并简单地给出“profileDirectory”路径

我得到的错误为:

Exception in thread "main" java.lang.NoClassDefFoundError: net/arnx/jsonic/JSONException
at shyjo_langDetect.LanguageCodeDetection_1.init(LanguageCodeDetection_1.java:21)
at shyjo_langDetect.LanguageCodeDetection_1.main(LanguageCodeDetection_1.java:40)
Caused by: java.lang.ClassNotFoundException: net.arnx.jsonic.JSONException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 2 more

C:\Users\geethu\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

我添加了必需的jar文件到图书馆还(http://www.java2s.com/Code/Jar/l/Downloadlangdetectjar.htm)由于我是新来的JAVA,我不明白为什么这样的错误来了。谁能帮我解决这个错误,使代码工作?

java noclassdeffounderror language-detection
1个回答
0
投票

请加jsonic-1.2.0.jar和langdetect.jar到NetBeans项目的构建路径。您可以您先前提供的GitHub的URL的lib目录下找到这两个JAR的。

变更后,你应该能够获得所需的输出:

[ru:0.9999987658571312]
ru
© www.soinside.com 2019 - 2024. All rights reserved.