Abdeali Chandanwala
Abdeali Chandanwala

Reputation: 8828

eclipse class path issue with jackson lib gradle

My project opened once in eclipse oxygen and in mars, I was shown an fix to import jackson lib in eclipse oxygen (a eclipse suggestion) - I did that and it fixed the issue there - although I already had mentioned the same dependency in my gradle dependencies list of my project.

After that I opened the same project in eclipse mars and the dependency is shown in buildpath(eclipse list of deps), in my gradle deps list but eclipse mars still is showing an error on source code.

I cleaned the project and did ./gradlew eclipse as well - everything is successfully but eclipse mars fails to resolve the dependency - how should I resolve this issue.

my build.gradle is

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java Library project to get you started.
 * For more details take a look at the Java Libraries chapter in the Gradle
 * user guide available at https://docs.gradle.org/3.5/userguide/java_library_plugin.html
 */

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'
apply plugin: 'eclipse'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:21.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    // https://mvnrepository.com/artifact/org.jsoup/jsoup
    compile group: 'org.jsoup', name: 'jsoup', version: '1.11.2'

    // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.9.3'


}

enter image description here

Here's my code for ref

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.Scanner;
import java.util.regex.Pattern;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class Main {

//  public ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) {
//      for (String html : args) {
//          Main main = new Main();
//          try {
//              main.parseHtml(html);
//          } catch (JsonProcessingException e) {
//              e.printStackTrace();
//          }
//      }

        Main main = new Main();

        String html = main.getFile("appleInc.html");
        try {
            main.parseHtml(html);
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private String getFile(String fileName) {

        StringBuilder result = new StringBuilder("");

        //Get file from resources folder
        ClassLoader classLoader = getClass().getClassLoader();
        File file = new File(classLoader.getResource(fileName).getFile());

        try (Scanner scanner = new Scanner(file)) {

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                result.append(line).append("\n");
            }

            scanner.close();

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

        return result.toString();

      }

    public void parseHtml(String html) throws JsonProcessingException {
        Document document = Jsoup.parse(html);
        // parse all the table required
//      ArrayNode tableInfo = retrieveTableInfo(document);

        // get the metadata from the html
        ObjectNode metadataObject = retrieveMetadataInfo(document);
//      tableInfo.add(metadataObject);
//      System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(tableInfo));
    }

    @SuppressWarnings("unused")
    private ObjectNode retrieveMetadataInfo(Document document) {
        String type = document.getElementsByTag("type").text();
        String companyName = findCompanyName(document);
        String employerIdentificationNo = findEmployerIdentificationNumber(document);
        return null;
    }

    private String findEmployerIdentificationNumber(Document document) {
        String employerNo = "";
        employerNo = document.getElementsContainingText("I.R.S. Employer").prev("div").text();
        if (employerNo.isEmpty()) {
            Iterator<Element> iterator = document.getElementsContainingText("Employer Identification").prev("tr")
                    .iterator();
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element.is("tr")) {
                    employerNo = element.getElementsMatchingText(getPattern()).text();
                }
            }
        }
        return null;
    }

    private Pattern getPattern() {
        String re1 = "(\\d+)";
        String re2 = "(-)";
        String re3 = "(\\d+)";

        return Pattern.compile(re1 + re2 + re3, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    }

    private String findCompanyName(Document document) {
        return document.getElementsContainingText("Exact name of Registrant").prev("div").text();
    }

    private ArrayNode retrieveTableInfo(Document document) {
        Elements tables = document.getElementsByTag("table");
        tables.forEach(table -> {
            if (Validator.isTableUsefull(table))
                return;
            String tableTitle = getTableTitle(document, table);

        });

        return null;
    }

    private String getTableTitle(Document document, Element table) {
        // TODO Auto-generated method stub
        return null;
    }
}

enter image description here

Upvotes: 0

Views: 1207

Answers (1)

JB Nizet
JB Nizet

Reputation: 691715

You're using classes that are not part of jackson core. As you can see by browsing the documentation, or simply by looking at the content of the jar file.

As you can see, the class JsonProcessingException from the package com.fasterxml.jackson.core, is found without problem. That's because it is part of jackson-core.

The classes that are missing are all in the package com.fasterxml.jackson.databind. To have them resolved, you need to add the needed library: com.fasterxml.jackson.core:jackson-databind.

Upvotes: 2

Related Questions