微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么java找不到twilio?

如何解决为什么java找不到twilio?

我在 Gradle 中使用 Eclipse。 Twilio 被列为依赖项。所有版本都是最新的。引用 Gradle 的代码不会编译并返回 Twilio 类不存在的错误消息。在线代码令人沮丧地过时了。我希望这里有人可以提供帮助。

我运行的代码非常基础:

    package SendAndReceiveSms;
    
    import com.twilio.Twilio;
    import com.twilio.rest.api.v2010.account.Message;
    import com.twilio.type.PhoneNumber;
    
    public class SmsSender {
        public static final String ACCOUNT_SID = "**Redacted**";
        public static final String AUTH_TOKEN = "**Redacted**";
        
        public static void main(String[] args) {
            Twilio.init(ACCOUNT_SID,AUTH_TOKEN);
            Message message = Message.creator(
                    new PhoneNumber("**redacted**"),new PhoneNumber("**redacted**"),"Message testing.")
                    .create();
            System.out.println(message.getSid());
            //sendMessage("**redacted**");
            }
        
        public void sendMessage(String numToSend) {
            String n = numToSend;
            Message message = Message.creator(
                    new PhoneNumber(n),new PhoneNumber("+**(redacted)**"),"Message testing.")
                    .create();
            System.out.println(message.getSid());
            }
        
    }
    dependencies {
        // This dependency is exported to consumers,that is to say found on their compile classpath.
        api 'org.apache.commons:commons-math3:3.6.1'
    
        // This dependency is used internally,and not exposed to consumers on their own compile classpath.
        implementation 'com.google.guava:guava:28.2-jre'
        
        //compile group: "com.twilio.sdk",name: "twilio",version: "7.45.+"
        //compile group: "com.sparkjava",name: "spark-core",version: "2.7.1"
        //compile group: "org.slf4j",name: "slf4j-simple",version: "1.7.21"
        
        implementation 'org.slf4j:slf4j-simple:1.7.+'
        implementation 'com.sparkjava:spark-core:2.5.+'
        implementation group: 'com.twilio.sdk',name: 'twilio',version: '8.11.0'
        //implementation 'com.twilio.sdk:twilio:7.+'
       // implementation 'com.twilio.sdk',version: '8.11.0'
        //runtimeOnly group: 'com.twilio.sdk',version: '8.11.0'
        // Use JUnit test framework
        testImplementation 'junit:junit:4.12'
    }

    Error code in Terminal:
    
    SmsSender.java:3: error: package com.twilio does not exist
    import com.twilio.Twilio;
                     ^
    SmsSender.java:4: error: package com.twilio.rest.api.v2010.account does not exist
    import com.twilio.rest.api.v2010.account.Message;
                                            ^
    SmsSender.java:5: error: package com.twilio.type does not exist
    import com.twilio.type.PhoneNumber;
                          ^
    SmsSender.java:12: error: cannot find symbol
                    Twilio.init(ACCOUNT_SID,AUTH_TOKEN);
                    ^
      symbol:   variable Twilio
      location: class SmsSender
    SmsSender.java:13: error: cannot find symbol
                    Message message = Message.creator(
                    ^
      symbol:   class Message
      location: class SmsSender
    SmsSender.java:14: error: cannot find symbol
                                    new PhoneNumber("..."),^
      symbol:   class PhoneNumber
      location: class SmsSender
    SmsSender.java:15: error: cannot find symbol
                                    new PhoneNumber("..."),^
      symbol:   class PhoneNumber
      location: class SmsSender
    SmsSender.java:13: error: cannot find symbol
                    Message message = Message.creator(
                                      ^
      symbol:   variable Message
      location: class SmsSender
    SmsSender.java:24: error: cannot find symbol
                    Message message = Message.creator(
                    ^
      symbol:   class Message
      location: class SmsSender
    SmsSender.java:25: error: cannot find symbol
                                    new PhoneNumber(n),^
      symbol:   class PhoneNumber
      location: class SmsSender
    SmsSender.java:26: error: cannot find symbol
                                    new PhoneNumber("..."),^
      symbol:   class PhoneNumber
      location: class SmsSender
    SmsSender.java:24: error: cannot find symbol
                    Message message = Message.creator(
                                      ^
      symbol:   variable Message
      location: class SmsSender
    12 errors
    error: compilation Failed

This shows where Twilio is in the project file structure.

解决方法

我认为您的项目设置没有任何问题。您可以将其与 this reference project 进行比较。

所以,我怀疑您看到的问题与 Eclipse 导入 Gradle 项目的方式有关。我不是一个普通的 Eclipse 用户,但我确实为这个问题尝试过。当我在上面的 repo 中导入项目时,我看到了与您类似的导入错误。我能够通过首先告诉 Eclipse 这是一个 Gradle 项目(项目上下文菜单 > 配置 > 添加 Gradle Nature)然后刷新来修复它们项目(项目上下文菜单 > Gradle > 刷新Gradle 项目)。我从 this answer 那里得到了这些步骤。然后一切正常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。