【问题标题】:Exception while using HTMLUnit in Java在 Java 中使用 HTMLUnit 时出现异常
【发布时间】:2013-12-16 13:45:27
【问题描述】:

我在我的 Java 应用程序中使用 HTMLUnit。

这段代码:

 final WebClient webClient = new WebClient(BrowserVersion.CHROME_16);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            try {
                final HtmlPage page = webClient.getPage("http://ufs.pt/forum/forum.php"); 

                } catch (FailingHttpStatusCodeException e) {
            e.printStackTrace();
            } catch (MalformedURLException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
            }

在控制台应用中工作正常。

但是当我尝试在 Window Builder 中使用它时:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;

import javax.swing.JFrame;
import javax.swing.JButton;

import junit.framework.Assert;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;


public class gui {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                gui window = new gui();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 */
public gui() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JButton btnNewButton = new JButton("New button");
    btnNewButton.setBounds(122, 60, 117, 25);
    frame.getContentPane().add(btnNewButton);

    btnNewButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {
            final WebClient webClient = new       WebClient(BrowserVersion.CHROME_16);
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setJavaScriptEnabled(false);
            try {
                final HtmlPage page = webClient.getPage("http://ufs.pt/forum/forum.php");

            } catch (FailingHttpStatusCodeException e) {

                e.printStackTrace();
            } catch (MalformedURLException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

            webClient.closeAllWindows();

        }
    });


}

}

它返回给我这个错误: http://pastebin.com/YV7QcaWM

你能帮帮我吗? :)

【问题讨论】:

标签: java swing htmlunit nosuchmethod


【解决方案1】:

反射方法调用失败。这可能意味着您的 CLASSPATH 中包含包含 org.apache.http.conn.ssl.SSLSocketFactory 类的 jar 版本不兼容。

在过去,我偶尔会发现 JBoss Tattletale 有助于在我的 CLASSPATH 上查找有问题的 jar。

【讨论】:

    【解决方案2】:

    好的,我想我已经找到了解决方案: 我刚刚将我的 HTMLUnit 代码扔给了其他(新)类构造函数,并在 actionPerformed 方法中调用了这个类。现在可以正常使用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多