为什么我使用 htmlunit 在这个 java 摆动表单中不断收到 NoClassDefFoundError 错误?

问题描述

我最近读了一篇关于网页抓取的文章,听起来很有趣,但我想在 Swing 表单中使用这些库。这是我正在建模的教程:https://www.scrapingbee.com/java-webscraping-book/ 我没有做太多更改,只是基本上允许用户将 URL 输入到文本框中。我不断返回的异常是 java.lang.NoClassDefError,当我查找它时,它被解释为有问题的类可以在编译时而不是运行时定位的场景。我已经在 htmlunit 库中包含了所有的 jar 文件,我可以轻松地导入有问题的 WebClient,但是,尽管它在底部说构建成功,但该应用程序从未真正打开过。下面是代码,以及我收到的错误消息。只是为了测试,我复制了代码,将其粘贴到 netbeans 中,引入了所有必要的 jar 文件,并且出现了关于 WebClient 类的完全相同的错误。无论如何,这是我使用的代码,一如既往,我非常感谢任何人花时间帮助我。

sol = solve(eqn,[FISH_O_SYM(1,1),FISH_O_SYM(2,2),FISH_O_SYM(1,SIGMA_O])

这是另一个类:

FISH_O_SYM =

[ o_1_1/sigma_o^2,o_1_2/sigma_o^2]
[ o_1_2/sigma_o^2,o_2_2/sigma_o^2]

Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.

Error in sym.getEqnsVars (line 56)
    checkVariables(vars);

Error in sym/solve>getEqns (line 429)
[eqns,vars] = sym.getEqnsVars(argv{:});

Error in sym/solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});

Error in solve_obs_O_1BIN_GOOD_but_with_issue_on_sigma_o_dev (line 66)
sol = solve(eqn,SIGMA_O])

这是我在 Netbeans 中编译时返回的错误

package webscrape;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.awt.Color;
import java.util.List;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;


/**
 *
 * @author glass
 */
public class JsoupTutorial extends javax.swing.JFrame {

    private String baseUrl;
    private final WebClient client = new WebClient();
    
    /**
     * Creates new form JsoupTutorial
     */
    public JsoupTutorial() {
        
        this.responseArea.setBackground(Color.BLACK);
        this.responseArea.setEditable(false);
        this.responseArea.setContentType("text/html");
        HTMLEditorKit htmlKit = (HTMLEditorKit) this.responseArea.getEditorKitForContentType("text/html");
     
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        title = new javax.swing.JLabel();
        urlTxtBox = new javax.swing.JTextField();
        enterTxt = new javax.swing.JLabel();
        enterBtn = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        responseArea = new javax.swing.JEditorPane();

        setDefaultCloSEOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        title.setText("Jsoup Testing");

        enterTxt.setText("Enter a website:");

        enterBtn.setText("Enter");
        enterBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                enterBtnActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(responseArea);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(342,342,342)
                        .addComponent(title))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(24,24,24)
                        .addComponent(enterTxt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,false)
                            .addComponent(jScrollPane1,javax.swing.GroupLayout.DEFAULT_SIZE,298,Short.MAX_VALUE)
                            .addComponent(urlTxtBox))
                        .addGap(50,50,50)
                        .addComponent(enterBtn)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(title)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urlTxtBox,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(enterTxt)
                    .addComponent(enterBtn))
                .addGap(18,18,18)
                .addComponent(jScrollPane1,192,javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(71,Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void enterBtnActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // Todo add your handling code here:
        baseUrl = this.urlTxtBox.getText();
        
        //disable the css and javascript -- sites heavy in Ajax calls,this code wont work on
        client.getoptions().setCssEnabled(false);
        client.getoptions().setJavaScriptEnabled(false);
        
        try(client) {
            HtmlPage page = client.getPage(baseUrl);
            List<HtmlElement> itemList = page.getByXPath("//tr[@class='athing']");
            if(itemList.isEmpty())
            {
                System.out.println("No item found!");
            }
            else
            {
                for(HtmlElement htmlItem : itemList)
                {
                    int position = Integer.parseInt(((HtmlElement)htmlItem.getFirstByXPath("./td/span")).asText().replace(".",""));
                    int id = Integer.parseInt(htmlItem.getAttribute("id"));
                    String thetitle = ((HtmlElement) htmlItem.getFirstByXPath("./td[not@valign='top')][@class='title']")).asText();
                    String url = ((HtmlAnchor) htmlItem.getFirstByXPath("./td[not(@valign='top')][@class='title']/a")).getHrefAttribute();
                    String author = ((HtmlElement) htmlItem.getFirstByXPath("./following-sibling::tr/td[@class='subtext']/a[@class='hnuser']")).asText();
                    int score = Integer.parseInt(((HtmlElement) htmlItem.getFirstByXPath("./following-sibling::tr/td[@class='subtext']/span@class='score']")).asText().replace(" points",""));
                    
                    HackerNewsItem hnItem = new HackerNewsItem(thetitle,url,author,score,position,id);
                    
                    ObjectMapper mapper = new ObjectMapper();
                    String jsonString = mapper.writeValueAsstring(hnItem);
                    
                    System.out.println(jsonString);
                    
                    
                    this.responseArea.setText("<html><head><title>Json Response Below:</title></head></html>");
                    
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(JsoupTutorial.class.getName()).log(Level.SEVERE,null,ex);
        } catch (FailingHttpStatusCodeException ex) {
            Logger.getLogger(JsoupTutorial.class.getName()).log(Level.SEVERE,ex);
        }
    }                                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available,stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JsoupTutorial.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JsoupTutorial.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (illegalaccessexception ex) {
            java.util.logging.Logger.getLogger(JsoupTutorial.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JsoupTutorial.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokelater(new Runnable() {
            public void run() {
                new JsoupTutorial().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton enterBtn;
    private javax.swing.JLabel enterTxt;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JEditorPane responseArea;
    private javax.swing.JLabel title;
    private javax.swing.JTextField urlTxtBox;
    // End of variables declaration                   
}

它就像在运行时无法定位 WebClient 类一样。它说构建成功,但 Jframe 从未出现在屏幕上,并且 NetBeans 通常表明我有一些东西正在运行,但不是这样。就好像编译了然后不飞一样。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...