我对 Gradle 还很陌生,所以我正在尝试构建一个 Java 项目,但不确定依赖项。我从未将 Gradle 配置为能够执行我的测试,也从未将 jar 文件配置为编译和运行。
我的 build.gradle
:
apply plugin: 'java'
apply plugin: 'maven'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.25'
compile 'org.json:json:20160212'
testCompile 'junit:junit:4.12'
}
这就是我在控制台上看到的内容,表明它没有看到我的导入:
error: package org.json.simple does not exist
import org.json.simple.JSONParser;
这是我的课:
import org.json.simple.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class FileLoader {
@SuppressWarnings("unchecked")
public static void main(String args[]) {
JSONParser parser = new JSONParser();
int count = 0;
try {
Object obj = parser.parse(new FileReader(
"Consumers.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray array = jsonObject.getJSONArray("people");
} catch (Exception e) {
e.printStackTrace();
}
}
}
最佳答案
如果您下载 JSON jar指定,并列出其内容(例如使用 jar tf
),它不包含 org.json.simple
包裹。
所以问题很简单,你需要另一个 jar 。
编辑:
我不知道这是否是意图,但有根据的猜测:如果您将此依赖项添加到 build.gradle
:
compile 'com.googlecode.json-simple:json-simple:1.1.1'
和这些进口:
import org.json.simple.parser.*;
// import org.json.simple.*;
import org.json.*;
然后这个例子编译(对我来说)。
关于Gradle依赖 json-simple 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46958715/