tags:

views:

89

answers:

3
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.sql.*;  
public class linksfind{
public static void main(){
    String html = "http://www.apple.com/pr/";
    Document document = Jsoup.parse(html); // Can also take an URL.
    for (Element element : document.getElementsByTag("a")) {
        System.out.println(element.attr("href"));
}
}
}

Guys, In the above program, while executing I find these errors. How to resolve? I have downloaded Jsoup.jar file in my folder location. What else should I do?

linksfind.java:8: cannot find symbol
symbol  : class Document
location: class linksfind
    Document document = Jsoup.parse(html); // Can also take a
    ^
linksfind.java:8: cannot find symbol
symbol  : variable Jsoup
location: class linksfind
    Document document = Jsoup.parse(html); // Can also take a
                        ^
linksfind.java:9: cannot find symbol
symbol  : class Element
location: class linksfind
    for (Element element : document.getElementsByTag("a")) {
A: 

Looks like you're missing the jsoup library from your classpath. Then you must import the required org.jsoup.* packages,

Jim Garrison
+6  A: 

The ones of Jsoup of course.

import org.jsoup.nodes.Document;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;

Also see the Jsoup API documentation.


That said, there's another problem which would only manifest when you got it run: you're passing the URL in flavor of a java.lang.String instead of a java.net.URL. A String would be treated as plain HTML, not as a resource. Fix it as well:

URL url = new URL("http://www.apple.com/pr/");
Document document = Jsoup.parse(url, 3000);

Update: you just need to ensure that Jsoup libraries are present in both the compiletime and runtime classpath. When using javac.exe and java.exe, use the -cp argument. E.g. to compile it:

javac -cp .;/path/to/jsoup.jar com/example/YourClass.java

and to execute it:

java -cp .;/path/to/jsoup.jar com.example.YourClass
BalusC
On adding these packages, i have got error saying these packages does not exist. What Should I do? Please advise
LGAP
The JAR file is apparently not been placed in the classpath. This problem goes beyond Jsoup. It's basic Java. Are you using an IDE like Eclipse/Netbeans or still fiddling it all plain in command console?
BalusC
Please advise.. where should I place that .jar file....
LGAP
Just in the classpath. Please answer my last question. The detailed answer depends on that.
BalusC
No I am not using IDE.. am jus executing a text file with .java extension..
LGAP
Thanks Balus for ur replies.. Please make me arrive at the result :)
LGAP
See answer update.
BalusC
Thank you so much Balus.... :) am done
LGAP
You're welcome. In future, try learning basic Java and learn how to read API documentation.
BalusC
yup sure.. :)..
LGAP
A: 

It looks like the jsoup.jar is not picked up correctly and missing during compilation. Jsoup only has one dependendy (commons lang), so other missing external dependencies don't seem to be the immediate problem in your case.

You might want to try out Maven or Ivy to resolve your dependencies if you don't want to do it manually.

bunting
I would not recommend Maven or Ivy to someone who didn't understand how to use CLASSPATH.
duffymo