【问题标题】:How to import external dependencies in gradle?如何在 gradle 中导入外部依赖项?
【发布时间】:2019-05-08 22:15:40
【问题描述】:

这里是 Java 开发的新手。我在eclipse中使用gradle。

我想导入 JSONParser。在我的代码中,我有:

    import org.json.simple.parser.JSONParser;

在 build.gradle 我有:

repositories {

    mavenCentral()
}

dependencies {

    compile 'com.googlecode.json-simple:json-simple:1.1.1'


}

但是,当我尝试构建时,我得到:

int/MainApp.java:7: error: cannot find symbol
import org.json.simple.parser;
                      ^
  symbol:   class parser
  location: package org.json.simple
1 error

这里发生了什么?我想我不明白 gradle 是如何工作的。

【问题讨论】:

  • 你能改吗compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1'

标签: java eclipse gradle


【解决方案1】:

试试吧,它可能对你有帮助:

 buildscript {
   ext {
     springBootVersion = '2.1.0.RELEASE'
   }
   repositories {
     mavenCentral()
   }
   dependencies {
     classpath("org.springframework.boot:spring-boot-gradle- 
     plugin:${springBootVersion}")
  }
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

 group = 'com.xyz'
 version = '0.0.1-SNAPSHOT'
 sourceCompatibility = 1.8

 repositories {
  mavenCentral()
 }

 dependencies {
   compile 'com.googlecode.json-simple:json-simple:1.1.1'
 }

【讨论】:

  • 我不确定 gradle 是如何工作的。我需要下载jar吗?
  • 你这是什么意思?
  • 意味着进入'Window->Preferences->General->Keys'并进行Build and Clean,它将清除并导入所有依赖项。
【解决方案2】:

如果你后来添加了依赖:

项目右键->gradle->刷新

【讨论】:

    【解决方案3】:

    如果你的 jar 文件在一个文件夹中,例如在根目录的 lib 文件夹中,将以下内容添加到你的 gradle 构建中

    dependencies 
    {
    compile files('libs/something_local.jar')
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-22
      • 2019-05-08
      相关资源
      最近更新 更多