简体   繁体   中英

Jsoup - The method connect(String) is undefined for the type Jsoup

So I'm trying to install Jsoup to Eclipse.

  1. Made a user library (Window->Preferences, Java->build path->user library, new->name("JsoupLibrary")->add JARs) the JARs. The JARs I downloaded from http://jsoup.org/download
  2. Build the path to my project. (right click project->build path->configure build path, add library->user library->next->JsoupLibrary-finish)

So i tried to run the example they gave on their website (see code) I could import Document and Elements. But it keeps giving an error on "connect". Am i doing something wrong?? Does anyone know how to fix this problem?

Error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    The method connect(String) is undefined for the type Jsoup

    at JsoupTesting.Jsoup.main(Jsoup.java:12)

Jsoup test:

package JsoupTesting;

import java.io.IOException;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Jsoup {

    public static void main(String[] args) {

        Document doc = Jsoup.connect("http://en.wikipedia.org/").get();
        Elements newsHeadlines = doc.select("#mp-itn b a");

    }

}

Problem is that your class is also named Jsoup so compiler in this code

Jsoup.connect("http://en.wikipedia.org/")

tries to use connect(String) method from your class, not from org.jsoup.Jsoup class, and since there is no such method in your class you see the error. To remove this problem change name of your class to something else like

public class JsoupDemo {
   ...
}

and add import to org.jsoup.Jsoup which has method you want to invoke.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM