1

I am using Eclipse with Gradle. Twilio is listed as dependency. All versions are up to date. Code referencing Gradle will not compile and returns error messages that the Twilio classes do not exist. Code online is frustratingly outdated. I'm hoping someone here can help.

The Code I am running is very basic:

    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', name: 'twilio', version: '8.11.0'
        //runtimeOnly group: 'com.twilio.sdk', name: 'twilio', 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.

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
Nilson
  • 11
  • 2

1 Answers1

0

I don't see any problem with your project setup. You can compare it to this reference project.

So, I suspect the problem you're seeing is related to how Eclipse imports Gradle projects. I'm not a regular Eclipse user but I did try it out for this question. When I imported the project in the above repo I saw import errors similar to yours. I was able to fix them by first telling Eclipse that this is a Gradle project (Project context menu > Configure > Add Gradle Nature) then refreshing the project (Project context menu > Gradle > Refresh Gradle Project). I got those steps from this answer. Then everything was working.

Matthew Gilliard
  • 9,298
  • 3
  • 33
  • 48
  • I'm relatively new to Eclipse myself, and this sounds like a promising solution. I'm working to follow your guidance and am not seeing the same menu selection that you have posted. I'm seeing Project context menu > Configure > Configure and detect nested projects. Is this where I need to be? – Nilson May 05 '21 at 17:25
  • I imported the reference code that you had posted and ran it unchanged. I'm still getting the Twilio library doesn't exist message. App.java:6: error: package com.twilio does not exist import com.twilio.Twilio; ^ App.java:23: error: cannot find symbol Twilio.init( ^ symbol: variable Twilio location: class App 2 errors – Nilson May 05 '21 at 19:05