1

I have a runtime error when i try to run my android application, due to the importation of this package that allows me to use Watson Services.

'com.ibm.watson.developer_cloud:java-sdk:2.9.0'

In fact, when I use something related to this package, the error that appears is:

E/dalvikvm: Could not find class 'javax.naming.InitialContext', referenced from method com.ibm.watson.developer_cloud.util.CredentialUtils.getKeyUsingJNDI

and the corresponding stacktrace is:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: semantic.myapplication, PID: 29120
java.lang.VerifyError: com/ibm/watson/developer_cloud/util/CredentialUtils
at….

I’ve looked for the real problem of this error and I’ve found that in the file “CredentialUtils.java” where the error appears, these three importations are not recognized (Cannot resolve symbols):

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

I use Android Studio and the latest version of jdk (1.8.0.77). These java files exist, I can see them but for some reason are not accessible: the importation of every file in javax.naming (and also other folders) is not possible.

How can I solve this problem? I’ve tried to import a .jar file including the javax.naming classes, but doesn’t work. Is there a way to make visible that classes?

Any help would be appreciated. Thanks!

Dieghitus
  • 55
  • 3
  • 9
  • There is an issue related to this: http://stackoverflow.com/questions/36217233/javax-content-not-accessible – German Attanasio Mar 26 '16 at 01:14
  • The link you have posted is the same of this discussion ^^ – Dieghitus Mar 26 '16 at 07:53
  • sorry I was talking about https://github.com/watson-developer-cloud/java-sdk/issues/167 – German Attanasio Mar 26 '16 at 16:19
  • Thank you for the link! I've downgraded the version of the package to 2.6.0 and I've no more this problem, because in that version the file CredentialUtils doesn't exist. However the entire package "javax.naming" (and many others of javax) continue to be not accessible and not usable. Is this a common issue? – Dieghitus Mar 26 '16 at 16:32
  • yeah, and it's my fault because I didn't verify the pull request that introduced that problem in android... – German Attanasio Mar 29 '16 at 19:24

1 Answers1

0

I think you are using a version older than 2.7.1. I'm unable to reproduce your problem and I tried with different configurations including yours. Make sure you clean your workspace and get the latest version of the library. In your build.gradle you should have:

compile 'com.ibm.watson.developer_cloud:java-sdk:2.9.0'

Here is the commit that fixed your issue. The fix was released in the 2.7.1 version.

This is what I did and that worked for me:

Steps to use the Watson APIs in Android you should:

  1. Download and install Android Studio
  2. Open Android Studio
  3. File > New > New Project.
  4. Pick a name and click next, next, next and Finish
  5. In <project-name>/app/build.gradle add:

    compile 'com.ibm.watson.developer_cloud:java-sdk:2.9.0'  
    
  6. In MainActiviy.java at the end of onCreate(...) add:

    Thread thread = new Thread(new Runnable(){
      @Override
      public void run() {
        try {
          SpeechToText service = new SpeechToText();
          service.setUsernameAndPassword("<username>", "<password>");
          System.out.println(service.getModels());
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });
    
    thread.start();
    
  7. Start the simulator and look at the console. You should see a message like

    I/CredentialUtils: JNDI string lookups is not available.
    

    and the Speech to Text model list.

    I/System.out: [{
    I/System.out:   "name": "en-US_NarrowbandModel",
    I/System.out:   "rate": 8000
    I/System.out: }, {
    I/System.out:   "name": "pt-BR_BroadbandModel",
    I/System.out:   "rate": 16000
    I/System.out: }, {
    I/System.out: .... more models here
    I/System.out: }, {
    I/System.out:   "name": "en-US_BroadbandModel",
    I/System.out:   "rate": 16000
    I/System.out: }]
    
German Attanasio
  • 22,217
  • 7
  • 47
  • 63
  • 1
    Thank you very much. I should have replied before your answer. I've recently discovered that my problem is more general, and not so related to Watson API. In fact, in Android Studio's project structure, SDK and JDK locations are correctly specified, but when I try to import "java" and "javax" packages, only a part of the subdirectories is visible. For example, "import javax." let me see only "crypto, microedition, net, security, sql, xml" subdirectories - all the others (es. naming) are not visible, as they are not correctly compiled, even if I can see them in the "External Libraries" folder. – Dieghitus Apr 07 '16 at 07:59
  • 1
    Also, I've tried to insert the path of the file into jdk folder as build.gradle dependency: "compile files('C:/Programmi/Java/jdk1.8.0_77/jre/lib/rt.jar')". Doing this I'm able to finally see all the subfolders of java and javax, but many problems arise when I try to run the app, maybe due to conflicts with the existing library rt.jar that has been compiled by default. So I've a problem more general uncorrelated to Watson Services. Sorry if in a first time I've incentrated the problem on Watson Services, but I noticed this problem only thanks to them :) – Dieghitus Apr 07 '16 at 08:17