简体   繁体   English

通过Maven将.jar文件添加到类路径

[英]Adding a .jar file to classpath through maven

I'm having some trouble in getting maven to download a number of .jar files my application depends on. 我在让Maven下载我的应用程序依赖的多个.jar文件时遇到了一些麻烦。 The code in which these dependencies are needed is bellow: 下面是需要这些依赖项的代码:

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

}

After reading this question, I tried adding the mentioned packages( jackson-databind , jackson-core ) to pom.xml : 阅读问题后,我尝试将提到的软件包( 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>

How can I add the respective dependencies? 如何添加各自的依赖关系?

Edit #1: 编辑#1:

The errors given are bellow(the lines where they occur are marked in the code above): 出现以下错误(在上面的代码中标记了发生错误的行):

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

Edit #2: 编辑#2:

I can't seem to add the dependencies: 我似乎无法添加依赖项:

依赖

For databind you want this: 对于数据绑定,您需要这样做:

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

What is missing? 什么东西少了? Can you post an error message or stack trace? 您可以发布错误消息或堆栈跟踪吗?

Here is a good site to find dependencies for maven. 这是一个找到maven依赖项的好站点。

Can you try with 2.5.4 version as below: 您可以尝试以下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>

In IntelliJ, try to tick a checkbox "export" in your dependencies . 在IntelliJ中,尝试在您的dependencies checkbox “导出” checkbox

I mean: Project Structure -> Modules -> Dependencies, and there you can see libs included to the module. 我的意思是:项目结构->模块->依赖关系,在那里您可以看到模块中包含的库。 You should also see a checkbox near each lib in column 'Export'. 您还应该在“导出”列中每个库附近看到一个checkbox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM