【问题标题】:Adding a .jar file to classpath through maven通过maven将.jar文件添加到类路径
【发布时间】:2015-09-27 21:30:51
【问题描述】:

我在让 maven 下载我的应用程序所依赖的一些 .jar 文件时遇到了一些麻烦。需要这些依赖的代码如下:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ServerConfiguration {
    public String info = null;
    public String idlURL = null;
    public String idlContents = null;
    public List<ServerInfo> servers = new ArrayList<>();

    public final void clear() {
        info = null;
        idlURL = null;
        idlContents = null;
        if (servers != null)
            servers.clear();
    }

    private final static ObjectReader jsonReader;
    private final static ObjectWriter jsonWriter;

    static {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // <== Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator class file for com.fasterxml.jackson.core.JsonGenerator not found
        //mapper.configure(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, true);
        jsonWriter = mapper.writer();
        jsonReader = mapper.reader(ServerConfiguration.class);
    }

    public static ServerConfiguration fromJson(String json) throws IOException {
        return jsonReader.<ServerConfiguration>readValue(json); // <== Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException class file for com.fasterxml.jackson.core.JsonProcessingException not found
    }

    public String toJson() throws IOException {
        return jsonWriter.writeValueAsString(this);
    }

}

阅读this问题后,我尝试将提到的包(jackson-databindjackson-core)添加到pom.xml

<dependencies>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>17.0</version>
    </dependency>
    <dependency>
        <groupId>io.netty</groupId>
        <artifactId>netty-all</artifactId>
        <version>4.0.21.Final</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.3.3</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr4-maven-plugin</artifactId>
        <version>4.2.2</version>
    </dependency>
</dependencies>

如何添加各自的依赖项?

编辑#1:

给出的错误如下(在上面的代码中标记了它们发生的行):

Error:(52, 15) java: cannot access com.fasterxml.jackson.core.JsonGenerator
  class file for com.fasterxml.jackson.core.JsonGenerator not found
Error:(54, 28) java: cannot access com.fasterxml.jackson.core.ObjectCodec
  class file for com.fasterxml.jackson.core.ObjectCodec not found
Error:(55, 28) java: cannot access com.fasterxml.jackson.core.Base64Variant
  class file for com.fasterxml.jackson.core.Base64Variant not found
Error:(59, 26) java: cannot access com.fasterxml.jackson.core.JsonProcessingException
  class file for com.fasterxml.jackson.core.JsonProcessingException not found
Error:(63, 26) java: cannot access com.fasterxml.jackson.core.Versioned
  class file for com.fasterxml.jackson.core.Versioned not found

编辑#2:

我似乎无法添加依赖项:

【问题讨论】:

    标签: java json maven intellij-idea dependencies


    【解决方案1】:

    你可以试试2.5.4 版本如下:

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.5.4</version>
            </dependency>
    
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.5.4</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.5.4</version>
            </dependency>
    

    在 IntelliJ 中,尝试在您的 dependencies 中勾选 checkbox“导出”。

    我的意思是:项目结构 -> 模块 -> 依赖项,在那里您可以看到模块中包含的库。您还应该在“导出”列中的每个库附近看到一个 checkbox

    【讨论】:

    • 添加依赖后你的代码编译了吗?
    • 是的,我已经用所有的依赖变体编译了它。
    • 更新了答案,你可以试试吗?
    • 我尝试添加依赖项,但得到更新问题中提到的错误。
    • 您可以尝试从 Intellij 外部构建您的项目吗?
    【解决方案2】:

    对于你想要的数据绑定:

    <dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-databind</artifactId>
     <version>2.3.3</version>
    </dependency>
    

    缺少什么?你能发布错误消息或堆栈跟踪吗?

    Here is a good site to find dependencies for maven.

    【讨论】:

    • 是的,我已经编辑了源代码。我已经在 pom.xml 中有那个条目。
    • 再次,您能否发布错误消息或堆栈跟踪以显示问题?
    猜你喜欢
    • 1970-01-01
    • 2016-04-09
    • 2016-07-10
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    相关资源
    最近更新 更多