1

I am trying to build a program that will take a sentence from user and return the language of the text to the user.

When I run my application, I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: net/arnx/jsonic/JSONException

Does this mean I have to download jsonic and add it as a jar file to my application?

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

import com.cybozu.labs.langdetect.LangDetectException; 

public class LangDetectSample {
    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 detectLangs(String text) throws LangDetectException {
        Detector detector = DetectorFactory.create();
        detector.append(text);
        return detector.getProbabilities();
    }

    public static void main(String[] args) throws LangDetectException {
        LangDetectSample detector = new LangDetectSample();
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter text: ");
        String input = scanner.nextLine();
        String language = detector.detect(input);
        System.out.print(language);
    }
}
bcsb1001
  • 2,834
  • 3
  • 24
  • 35
karir
  • 25
  • 1
  • 7
  • 1
    `NoClassDefFoundError` typically means you are missing a library at runtime from your classpath, yes. – OneCricketeer Jun 26 '16 at 15:21
  • Read the required section here. Or, alternatively, use Maven/Gradle to manage your dependencies. https://github.com/shuyo/language-detection/blob/wiki/ProjectHome.md – OneCricketeer Jun 26 '16 at 15:23
  • that is great cricket_007 thank you, have you used this language identification library before? – karir Jun 26 '16 at 15:26
  • Nope, just really good at Googling – OneCricketeer Jun 26 '16 at 15:27
  • that's what I should be good at. I added jsonic but now it says Exception in thread "main" com.cybozu.labs.langdetect.LangDetectException: need to load profiles which I think is referring to the list of languages in a folder called profiles, I have added this folder just like I add a jar folder but I did not fix the problem – karir Jun 26 '16 at 15:33
  • @karir This is a an old question but it still deserves an answer: There's a [tutorial](https://code.google.com/archive/p/language-detection/wikis/Tutorial.wiki). You have to give it the path to the json files in code (`DetectorFactory.loadProfile(profileDirectory)`), don't add it to the build path. Btw, this library still works in 2021 (Java 8) but it seems to have problems detecting multiple languages in the same text and just returns the most common one with a 99% probability. – Neph Jan 29 '21 at 10:57

1 Answers1

2

NoClassDefFoundError happens if your class definition is present at compile time but is missing at runtime. You need to provide your program with a library that contains the definition.

To do that you usually need to place your library right next to your program because that's where your program will search for it.

Shiro
  • 2,610
  • 2
  • 20
  • 36