Microsoft Translator API in Java application

2019-08-01 08:37发布

问题:

I'm trying to make Microsoft Translator API in my Java application. I registered in Windows Azure market place and I registered my Microsoft Translate application so I have these ClientId and Client Secret keys but still it doesn't work.

I can't find what I am doing wrong.

Here is my code:

import com.memetix.mst.language.Language;
import com.memetix.mst.translate.Translate;

public class Main {
    public static void main(String[] args) {
        try{
            Translate.setClientId(/* my Client Id */);
            Translate.setClientSecret(/* my Client Secret */);

            String translatedText = Translate.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH);

            System.out.println(translatedText);
        }
        catch(Exception e) {
            System.err.println("Exception: " + e.getMessage());
        }
    }
} 

And this is the error I'm getting:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONValue
at     com.memetix.mst.MicrosoftTranslatorAPI.retrieveResponse(MicrosoftTranslatorAPI.java:161)
at com.memetix.mst.MicrosoftTranslatorAPI.retrieveString(MicrosoftTranslatorAPI.java:199)
at com.memetix.mst.translate.Translate.execute(Translate.java:61)
at dictionary.mainWindow.main(mainWindow.java:27)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONValue
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 4 more
Java Result: 1

回答1:

You are missing json-simple.jar on your classpath: http://search.maven.org/remotecontent?filepath=com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar