必须导入哪些软件包? [英] Which packages must be imported?

查看:87
本文介绍了必须导入哪些软件包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"));
}
}
}

伙计们, 在上面的程序中,执行时我发现了这些错误.怎么解决?我已经在文件夹位置下载了Jsoup.jar文件.我该怎么办?

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")) {

推荐答案

当然是Jsoup的.

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

另请参见 Jsoup API文档.

也就是说,还有一个问题只有在运行时才会显现:您以java.lang.String而不是java.net.URL的形式传递URL. String将被视为纯HTML,而不是资源.还要修复它:

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);


更新:您只需要确保Jsoup库同时存在于编译时和运行时类路径中即可.使用javac.exejava.exe时,请使用-cp自变量.例如.进行编译:


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

并执行它:

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

这篇关于必须导入哪些软件包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆