简体   繁体   中英

How to resolve java.lang.NoClassDefFoundError in the following code?

I am new to NLP and JAVA. Recently I started working on language detection and i got a code from How to detect language of user entered text? . I am using NetBeans 8.2 and copied the following code in it:

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) {
    }
}

}

"profiles" folder, I just download from github( https://github.com/shuyo/language-detection ) and simply given the path in "profileDirectory"

I am getting the error as:

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)

I added the required jar file into the library also( http://www.java2s.com/Code/Jar/l/Downloadlangdetectjar.htm ) Since I am new to JAVA, I couldn't understand why this error is coming. Can anyone help me to resolve the error and make the code working?

Please add the jsonic-1.2.0.jar and langdetect.jar into the Build path of your NetBeans project. You can find both these Jar's under the lib directory of the GitHub URL which you had provided earlier.

Post change, you should be able to get the desired output:

[ru:0.9999987658571312]
ru

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