通过maven添加一个.jar文件到classpath [英] Adding a .jar file to classpath through maven

查看:1297
本文介绍了通过maven添加一个.jar文件到classpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,让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); //< ==错误:(52,15)java:无法访问com.fasterxml.jackson.core.JsonGenerator类文件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); //< ==错误:(59,26)java:无法访问com.fasterxml.jackson.core.JsonProcessingException类文件为com.fasterxml.jackson.core.JsonProcessingException未找到
}

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

}

阅读这个问题,我尝试添加( jackson-databind jackson-core )到 pom.xml

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

如何添加各自的依赖关系?



编辑#1:



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

 错误:(52,15)java:无法访问com.fasterxml.jackson.core.JsonGenerator 
类文件。没有找到fastxml.jackson.core.JsonGenerator
错误:(54,28)java:无法访问com.fasterxml.jackson.core.ObjectCodec
com.fasterxml.jackson.core.ObjectCodec的类文件不发现
错误:(55,28)java:无法访问com.fasterxml.jackson.core.Base64Variant
类文件为com.fasterxml.jackson.core.Base64Variant未找到
错误:( 59,26)java:无法访问com.fasterxml.jackson.core.JsonProcessingException
类文件为com.fasterxml.jackson.core.JsonProcessingException未找到
错误:(63,26)java:无法访问com.fasterxml.jackson.core.Versioned
com.fasterxml.jackson的类文件。 core.Versioned not found

编辑#2:



我似乎无法添加依赖关系:



解决方案

你可以尝试使用 2.5.4 版本如下:

 <依赖关系> 
< 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>
<依赖关系>
< groupId> com.fasterxml.jackson.core< / groupId>
< artifactId> jackson-annotations< / artifactId>
< version> 2.5.4< / version>
< / dependency>

在IntelliJ中,尝试勾选复选框我的意思是:项目结构 - >模块 - >依赖关系,在那里你的依赖关系中的导出可以看到模块中包含的库。您还应在导出列中的每个库附近看到复选框


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:

解决方案

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

这篇关于通过maven添加一个.jar文件到classpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆