我的项目在 eclipseoxy 和 mars 中打开一次,我看到了在 eclipseoxy 中导入 jackson lib 的修复(eclipse 建议) - 我这样做了,它解决了那里的问题 - 尽管我已经提到了相同的依赖项在我的项目的 gradle 依赖项列表中。
之后,我在 eclipse mars 中打开了相同的项目,并且依赖项显示在我的 gradle deps 列表中的 buildpath(eclipse list of deps) 中,但 eclipse mars 仍然在源代码上显示错误。
我清理了项目并做了 ./gradlew eclipse - 一切都很成功,但 eclipse mars 无法解决依赖关系 - 我应该如何解决这个问题。
我的build.gradle是
/*
* 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'
}
这是我的引用代码
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;
}
}
最佳答案
您使用的类不属于 jackson 核心。浏览the documentation可以看到,或者简单地通过查看 jar 文件的内容。
如您所见,来自包 com.fasterxml.jackson.core
的类 JsonProcessingException
被发现没有问题。那是因为它是 jackson-core 的一部分。
缺少的类都在com.fasterxml.jackson.databind
包中。要解决这些问题,您需要添加所需的库:com.fasterxml.jackson.core:jackson-databind
。
关于java - Jackson lib gradle 的 Eclipse 类路径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47893144/