How to import external dependencies in gradle?

白昼怎懂夜的黑 提交于 2020-03-05 04:47:25

问题


New to java development here. I'm using gradle in eclipse.

I want to import the JSONParser. In my code I have:

    import org.json.simple.parser.JSONParser;

and in build.gradle I have:

repositories {

    mavenCentral()
}

dependencies {

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


}

However, when I try to build I get:

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

What's going on here? I think I don't understand exactly how gradle works.


回答1:


If you have the jar files in a folder, for example in the lib folder in root directory, add the following to your gradle build

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



回答2:


Try it, it may helpful for you:

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



回答3:


If you added dependency later:

Right click on project -> gradle -> refresh



来源:https://stackoverflow.com/questions/53664575/how-to-import-external-dependencies-in-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!