Getting compile error: package com.twilio.sdk does not exist

家住魔仙堡 提交于 2019-12-11 11:22:11

问题


New-bee in java here. I'm on an ubuntu 12.04 machine.

I am trying the Twilio API using java to make voice calls from an uiautomator test case and following the instructions provided at https://www.twilio.com/docs/java/install. I downloaded both twilio-java-sdk-3.4.2-with-dependencies.jar and twilio-java-sdk-3.4.2.jar from http://search.maven.org/#browse|1260284827 (pre-built).

I am using Twilio API in an uiautomator java project. I am able to build and run that uiautomator java project without implementing the Twilio API code. But if I try to use the Twilio API library, I get a compile time errors that it could not find the package.

Steps I'm doing:

1-> Open the java project in eclipse

2-> Add the Twilio java library twilio-java-sdk-3.4.2-with-dependencies.jar OR twilio-java-sdk-3.4.2.jar via BuildPath->Configure Build Path->Add External JARs.

I have following line of code to test if I can make the TwilioRestClient object. I have other test functions with the uiautomator and they work fine without this piece of code. Consider the following method in addition to other test methods.

test.java

//Assume all other required libraries are imported
import com.twilio.sdk.TwilioRestClient;

public class testClient extends UiAutomatorTestCase {   

    public void testMethodGetClient(){
        try{
            TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");
            log.info("client: " + client.getAccountSid());
        }catch(Exception e){
            log.info(e.toString());
        }
    }
}

I do not get any reference errors in my code before the comiple/build command. To believe that, if I do client. ,eclipse shows me all the methods available for the client object. So, can I assume here that my import was successful? Then I go the terminal and execute below command to create the build.xml file:

ubuntu terminal

$> android create uitest-project -n JARNAME -t 1 -p <PATH-TO-PROJECT>
$> ant clean build
Buildfile: <PATH-TO-PROJECT>/build.xml

-check-env:
 [checkenv] Android SDK Tools Revision 22.3.0
 [checkenv] Installed at <ANDROID-SDK-PATH>

-pre-clean:

clean:
   [delete] Deleting directory <PATH-TO-PROJECT>/bin

-check-env:
 [checkenv] Android SDK Tools Revision 22.3.0
 [checkenv] Installed at <ANDROID-SDK-PATH>

-build-setup:
[getbuildtools] Using latest Build Tools: 19.0.0
     [echo] Resolving Build Target for <PACKAGE-NAME>...
[getuitarget] Project Target:   Android 4.2.2
[getuitarget] API level:        17
     [echo] ----------
     [echo] Creating output directories if needed...
    [mkdir] Created dir: <PATH-TO-PROJECT>/bin
    [mkdir] Created dir: /<PATH-TO-PROJECT>/bin/classes

-pre-compile:

compile:
    [javac] Compiling 33 source files to <PATH-TO-PROJECT>/bin/classes
    [javac] <PATH-TO-PROJECT>/test.java:15: package com.twilio.sdk does not exist
    [javac] import com.twilio.sdk.TwilioRestClient;
    [javac]                      ^
[javac] <PATH-TO-PROJECT>/test.java:42: cannot find symbol
[javac] symbol  : class TwilioRestClient
[javac] location: class <packagename>.Telephony
[javac]             TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");
[javac]             ^
[javac] <PATH-TO-PROJECT>/test.java:42: cannot find symbol
[javac] symbol  : class TwilioRestClient
[javac] location: class <packagename>.Telephony
[javac]             TwilioRestClient client = new TwilioRestClient("ACCOUNT_SID", "AUTH_TOKEN");
[javac]                                           ^
    [javac] 3 errors

BUILD FAILED
<ANDROID-SDK-PATH>/tools/ant/uibuild.xml:183: Compile failed; see the compiler error output for details.

Total time: 1 second

The above command would create the .jar if I did not have the testMethodGetClient method. So, I serached for the articals for package not found errors, but most of them suggesting to add the library either via 'Add External Jars' or 'provide the class path'. I tried both and I get the same error. So, I came here and posting it as a new question.

Any help is greatly appreciated.

Regards, Rumit


回答1:


The default location for third party jars is normally the libs folder at the base of your project, i.e. <PATH-TO-PROJECT>.




回答2:


bgossit's answer did help me going to the right direction. However, that alone was not enough. So, answering my question by my self explaining a little extra step I had to do.

My first bottle neck is resolved, that is to compile the project successfully. However, I am still not able to run the project on an Android device. I am marking this comment as an answer as the question was for the complie error. I am puting a new question for the runtime error.

I found that the uiautomator's 'android create project' command creates a 'build.xml' which internally calls '<ANDROID_SDK>/sdk/tools/ant/uibuild.xml' to build the project and create the final jar. By default, the 'compile' target in this 'uibuild.xml' does not have a 'classpath' inlcuded for the jar. This seems to be the bug with Android as mentioned on couple of other StackOverflow questions/answers.

Solution that worked for me:

  1. Create the 'libs' directory at the same level as your 'src' directory and put your external jars into 'libs' directory. Note: This is just a good practice. Ideally you can give any name to the 'libs' directory and put it wherever you want.

  2. Add the 'classpath' attribute to the 'compile' target in <ANDROID_SDK>/sdk/tools/ant/uibuild.xml.

<classpath>
   <fileset dir="${jar.libs.dir}" includes="*.jar" /> <!-- path to 'libs' -->
</classpath>

PLEASE NOTE: Although, above solution worked to compile and build my project, I'm still not able to run it on the Android device as the Android device would not have Twilio API jar. I am looking for the ways to build the final project jar with Twilio jar/classes. java.lang.ClassNotFoundException for package com.twilio.sdk

Regards,

Rumit



来源:https://stackoverflow.com/questions/21588186/getting-compile-error-package-com-twilio-sdk-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!