在netbeans中使用外部jar依赖

时间:2018-03-20 21:12:52

标签: java maven spring-boot

我正在尝试使用Springboot在Netbeans上创建一个Web应用程序,它将生成所需的所有必要依赖项。我的项目将使用一个外部API,我将其转换为https://github.com/coinapi/coinapi-sdk/tree/master/java-rest的单个jar类,它被编译成多个类文件,然后编译成一个名为CoinAPI的jar文件。然后我通过

将jar添加到我的本地存储库
mvn install:install-file -Dfile=.\com\coinapi\CoinAPI.jar -DgroupId=com.coinapi
-DartifactId=CoinApi -Dversion=1.0.1 -Dpackaging=jar

已成功添加,然后将此jar添加到显示

的pom.xml中
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.alyxdev</groupId>
    <artifactId>CryptoStat</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>CryptoStat</name>
    <description>CryptoCurrency Analytics</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M8</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws-messaging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

        <dependency>
          <groupId>com.coinapi</groupId>
          <artifactId>CoinAPI</artifactId>
          <version>1.0.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>2.7.8</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

它成功构建,因此我尝试使用以下内容利用外部库中的一些类:

package com.alyxdev.cryptostat;

import com.coinapi.java_rest_coin_api;



/**
 *
 * @author Alyx
 */
public class CoinAPIConnector {

    public CoinAPIConnector(){

    }
    public void initConnect(){

        java_rest_coin_api connect = new java_rest_coin_api("BEA6B82A-2489-4B5F-8B28-157A627DC8A1");

    }

}

但由于某种原因,我的导入错误导致&#34;无法找到符号:class java_rest_coin_api location:package com.coinapi&#34;

我想知道在导入时我是否错误地使用了外部依赖项,或者我的导入是不正确的。

2 个答案:

答案 0 :(得分:0)

第一眼看,你的pom文件看起来不错。 因此,我建议在NetBeans中打开Project视图,找到 Dependencies 项。 然后,您可以单击它,并搜索您的CoinAPI库。然后你可以扩展它并找出所需的类是否在列表中。

还有另一个问题。 CoinAPI使用其他库:json-20140107.jar,okhttp-3.8.1.jar,okio-1.13.0.jar。

我敢打赌你需要将它们作为依赖项添加到你的pom文件中。幸运的是,它们中的大多数已经发布在中央maven repo上(至少是好的库)。

答案 1 :(得分:0)

Ok so I found the solution to add the library as a dependency. I created a new java library project on netbeans (instead of building the jar on the command line and externally adding it to the project with mvn install) then I added the sources files there and then built the project to create the jar files. I then modified my pom.xml to an updated version of the project and finally went to the dependencies right click on the the jar and clicked on manually install artifact and found the jar on my netbeans workbench. After that I just had to refresh my project and the errors were gone.