Adding a .jar file to classpath through maven

2019-08-08 09:01发布

问题:

I'm having some trouble in getting maven to download a number of .jar files my application depends on. 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:

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

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:

I can't seem to add the dependencies:

回答1:

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.



回答2:

Can you try with 2.5.4 version as below:

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

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