Search code examples
javaexceptioninstancepom.xmlnetbeans-8.2

i'm using HtlmUnit, and when consulting a web page I get Exception in thread "AWT-EventQueue-0" java.lang.NoSuchFieldError: INSTANCE"


I have not been able to read the content of a web page using the htmlUnit libraries that I installed from sourgeforce. I need help to solve the problem because I have tried to reinstall the libraries, modify the pom file, without obtaining any modification in the result. Other answers seem to point to the fact that it is a consequence of incompatibility in the versions of the components, but using previous versions of htmlunit has not worked either. This is the exception:

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

The code

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

using netbeans 8.2/JDK 1.8/HTLMUnit 4.0.0 and the POM.xlm:


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

How can i resolve this error.! Thanks


Solution

  • You're somehow picking up another version of HttpComponents. I don't think your POM is complete because it doesn't show your dependency on jsoup so I can't guess why you have got multiple versions of HttpComponents. Take a look at your dependency tree to see what version of and why each library is in use. Run mvn dependency:tree; to filter the results see this example. You'll need the dependency plugin to generate the dependency tree.

    A web search for "SSLConnectionSocketFactory nosuchfielderror instance" might provide some solutions, though the cause is generally the same when this exception occurs: picking up an unexpected version of HttpComponents.

    I don't believe this is related to your problem but it's worth bringing to your attention. You said you're on Java 8 and your POM says differently:

    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    

    Finally, NetBeans is up to version 21 so an update might be in order. :)