You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HtmlUnit调用网页出现NoSuchFieldError:INSTANCE异常的解决求助

问题

使用从SourceForge安装的HtmlUnit库无法读取网页内容,尝试重装库、修改pom文件、更换HtmlUnit旧版本均未解决问题。

报错信息

Conectando pagina Seniat
0 [AWT-EventQueue-0] DEBUG org.htmlunit.WebClient  - Get page for window named '', using WebRequest[<url="http://contribuyente.seniat.gob.ve/BuscaRif/BuscaRif.jsp", GET, EncodingType[name=application/x-www-form-urlencoded], [], {Accept=text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7, Accept-Encoding=gzip, deflate, br}, null>]
0 [AWT-EventQueue-0] DEBUG org.htmlunit.WebClient  - Load response for GET http://contribuyente.seniat.gob.ve/BuscaRif/BuscaRif.jsp
16 [AWT-EventQueue-0] DEBUG org.htmlunit.WebWindowImpl  - destroyChildren
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchFieldError: INSTANCE
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:151)
    at org.htmlunit.HttpWebConnection.configureHttpsScheme(HttpWebConnection.java:671)
    at org.htmlunit.HttpWebConnection.createHttpClientBuilder(HttpWebConnection.java:589)
    at org.htmlunit.HttpWebConnection.getHttpClientBuilder(HttpWebConnection.java:545)
    at org.htmlunit.HttpWebConnection.getResponse(HttpWebConnection.java:172)
    at org.htmlunit.WebClient.getWebResponseOrUseCached(WebClient.java:1654)
    at org.htmlunit.WebClient.loadWebResponseFromWebConnection(WebClient.java:1559)
    at org.htmlunit.WebClient.loadWebResponse(WebClient.java:1484)
    at org.htmlunit.WebClient.getPage(WebClient.java:451)
    at org.htmlunit.WebClient.getPage(WebClient.java:366)
    at org.htmlunit.WebClient.getPage(WebClient.java:504)
    at org.htmlunit.WebClient.getPage(WebClient.java:486)
    at com.openbravo.pos.smart.ConsultarSENIAT.consultarSENIAT(ConsultarSENIAT.java:73)
    at com.openbravo.pos.customers.CustomersView.jTextArea1MouseClicked(CustomersView.java:2186)
    at com.openbravo.pos.customers.CustomersView.access$900(CustomersView.java:69)
    at com.openbravo.pos.customers.CustomersView$8.mouseClicked(CustomersView.java:1305)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
...

相关代码

package com.openbravo.pos.smart;
import java.awt.image.BufferedImage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.htmlunit.WebClient;
import org.htmlunit.html.HtmlPage;


/**
 *
 * @author Smart Office Factory
 */

public class ConsultarSENIAT {
public  Element CI;
public  Element RIF;
public  BufferedImage Captcha;
public  Element Boton;
public  Element Codigo;
public Document doc;

public ConsultarSENIAT(){   
    this.CI=CI;
    this.RIF=RIF;
    this.Captcha=Captcha;
    this.Boton=Boton;
    this.Codigo=Codigo;
}

    public String consultarSENIAT() {
        
       
        System.out.println("Conectando pagina Seniat");
        String Resultado="";
        
        try (final WebClient webClient = new WebClient()) {
            webClient.getOptions().setJavaScriptEnabled(true); // Habilitar JavaScript si es necesario
            webClient.getOptions().setCssEnabled(true); // Habilitar CSS si es necesario

            HtmlPage page = webClient.getPage("http://contribuyente.seniat.gob.ve/BuscaRif/BuscaRif.jsp");
            
            webClient.waitForBackgroundJavaScript(10000); // Esperar a que se cargue el JavaScript en segundo plano
            webClient.waitForBackgroundJavaScriptStartingBefore(10000); // Esperar a que comience la carga del JavaScript en segundo plano*/

            // Aquí puedes realizar acciones adicionales una vez que la página ha cargado completamente
            System.out.println("Título de la página: " + page.getTitleText());
            // Agrega tu código adicional aquí

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

开发环境与pom.xml配置

  • 开发环境:NetBeans 8.2、JDK 1.8、HtmlUnit 4.0.0
  • pom.xml内容:
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>UnicentaSmarT</groupId>
  <artifactId>UnicentaSmarT</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
  </properties>
  <dependencies>
    <dependency>
      <groupId>net.sourceforge.htmlunit</groupId>
      <artifactId>htmlunit</artifactId>
      <version>4.0.0</version>
    </dependency>
    <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-1.2-api</artifactId>
    <version>2.17.2</version>
</dependency>
  </dependencies>
</project>

解决方法

这个NoSuchFieldError: INSTANCE异常是Apache HttpClient版本冲突导致的,HtmlUnit 4.0.0依赖特定版本的HttpClient,同时项目编译配置与JDK版本不匹配,具体解决步骤如下:

1. 修正JDK编译版本配置

开发环境用的是JDK 1.8,但pom.xml中设置的编译目标为11,会导致运行环境不兼容,修改pom.xml的properties部分:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

2. 强制指定兼容的HttpClient版本

HtmlUnit 4.0.0依赖HttpClient 4.5.14版本,在pom.xml中显式添加相关依赖,避免版本冲突:

<dependencies>
    <!-- HtmlUnit依赖 -->
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>4.0.0</version>
    </dependency>
    <!-- 显式指定HttpClient版本,解决冲突 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.14</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.16</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.5.14</version>
    </dependency>
    <!-- log4j依赖保留 -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>2.17.2</version>
    </dependency>
</dependencies>

3. 清理并重新构建项目

  • 在NetBeans中执行Clean and Build操作,清除旧编译文件和依赖缓存
  • 删除项目本地Maven仓库(.m2/repository)中HtmlUnit和HttpClient相关目录,避免残留旧版本

4. 排查依赖冲突

执行Maven命令查看依赖树,检查是否有其他依赖引入了旧版本HttpClient:

mvn dependency:tree

如果发现冲突,在对应依赖中添加排除规则:

<dependency>
    <!-- 冲突的依赖 -->
    <groupId>xxx</groupId>
    <artifactId>xxx</artifactId>
    <version>xxx</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </exclusion>
    </exclusions>
</dependency>

5. 降级HtmlUnit版本(可选)

若上述方法无效,可尝试降级到与JDK 1.8更兼容的HtmlUnit版本,比如3.60.0:

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>3.60.0</version>
</dependency>

内容的提问来源于stack exchange,提问作者Grey Molinaro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 22:12:04