我有 gradle.build 文件,我得到 Gson 库。那么,问题是:gradle下载gson.jar还是我需要手动完成?
这是我的 build.gradle :
apply plugin: 'java'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.8.2'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
但执行以下代码后失败,因为它无法找到 Gson :
private static class StoreHandler implements HttpHandler {
Gson gson = new Gson();
@Override
public void handle(HttpExchange httpExchange) throws IOException {
String data = "hi!";
httpExchange.getResponseBody().write(gson.toJson(data).getBytes());
}
}
Stacktrace正在关注(ClassNotFoundException:com.google.gson.Gson):
/usr/lib/jvm/java-8-oracle/bin/java -Didea.launcher.port=7533 -Didea.launcher.bin.path=/home/nazariy/idea/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/nazariy/Desktop/g/preproduction_practice/store/build/classes/java/main:/home/nazariy/idea/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain com.epam.moshenskyi.server.MainServer
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gson/Gson
at com.epam.moshenskyi.server.http.HttpServerImpl$StoreHandler.<init>(HttpServerImpl.java:31)
at com.epam.moshenskyi.server.http.HttpServerImpl$StoreHandler.<init>(HttpServerImpl.java:30)
at com.epam.moshenskyi.server.http.HttpServerImpl.run(HttpServerImpl.java:22)
at com.epam.moshenskyi.server.MainServer.main(MainServer.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
更新: 如果我重新创建项目添加从一开始就添加Gson依赖项,一切正常并且Gson正常工作,但如果我向现有项目添加新依赖项,则抛出ClassNotFound。可能它不能用我的项目重新创建jar文件并将Gson放在那里?
答案 0 :(得分:0)
根据您工作的环境,只需向({1}}添加依赖项,IDE可能会自动处理(不)。请尝试从命令行运行build.gradle
- 它应该在那里工作,假设您在源代码中输入正常。
使用控制台提供的输出进行编辑后,现在很清楚 - 您的类路径不包含gradle下载的依赖项。您需要将它们添加到类路径中。
答案 1 :(得分:-1)