简体   繁体   中英

java.lang.NoClassDefFoundError when Translating

I am using the Apertium Translator and using the sample code they provide. My code is like this.

import com.robtheis.aptr.language.Language;
import com.robtheis.aptr.translate.Translate;

public class Test {

public static void main(String[] args) throws Exception {
    // Set the Apertium API Key - Get yours at http://api.apertium.org/register.jsp
    Translate.setKey("BNSCFhEL8DoTApc2I1+aa3UYkVg");

    String translatedText = Translate.execute("Hola, mundo!", Language.SPANISH, Language.ENGLISH);

    System.out.println(translatedText);
}
}

I have no errors or warnings and when I run the program I get the following errors.

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONValue
at com.robtheis.aptr.ApertiumTranslatorAPI.jsonSubObjToString(ApertiumTranslatorAPI.java:195)
at com.robtheis.aptr.ApertiumTranslatorAPI.retrieveSubObjString(ApertiumTranslatorAPI.java:140)
at com.robtheis.aptr.translate.Translate.execute(Translate.java:56)
at maple.Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONValue
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more

The .jar I'm using is the second one from https://github.com/rmtheis/apertium-translator-java-api/downloads

You have to download the first one. The first jar file ( apertium-translator-java-api-0.2-jar-with-dependencies.jar ) contains the all dependencies needed..

Or you add a json library to your project path..

Well JVM is not able to find some class at Runtime which was available at compile time. That is the reason of NoClassDefFoundError
Below you also have ClassNotFoundException for this class org.json.simple.JSONValue which means you are trying to load the particular class at run-time by name.
Both of them related to Java Class Path . I don't know alot about jSON stuff. But you are missing this file org.json.simple.JSONValue you can take it from here http://code.google.com/p/json-simple/
add the above jar file in your class path . And the code will definitely run. Guaranteed.!!!
Thanks

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