帮助使用 Maven 构建和运行 Java 项目
我想尝试本文中的示例代码: 将 Recovery.gov 授权数据加载到 CouchDB 数据库。我熟悉 Java 语言,但对 Maven 构建系统一无所知。
我已成功通过运行“mvn package”来构建项目,但在尝试运行它时出现以下情况:
djm@surfer:~/source/sample-parse$ java -cp target/sample-parse-1.0-SNAPSHOT.jar com.discursive.sample.parse.LoadingGrants
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
我需要做其他事情来正确设置我的 CLASSPATH 吗?
谢谢!
I want to try out the example code from this article: Load Recovery.gov Grant Data into a CouchDB Database. I'm familiar with the Java language but I know nothing about the Maven build system.
I've managed to build the project by running 'mvn package', but I get the following when trying to run it:
djm@surfer:~/source/sample-parse$ java -cp target/sample-parse-1.0-SNAPSHOT.jar com.discursive.sample.parse.LoadingGrants
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Do I need to do something else to set up my CLASSPATH properly?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正确构建了 jar,但是当您运行代码时,您需要将类路径添加到包含 org.apache.httpcomponents 的 jar 中。如果您想将其打包为一个 jar,请使用 maven 程序集插件
带有依赖项的 jar
。顺便说一下,如果您不知道这一点,maven 按照惯例会将存储库 jar 文件下载到您的
.m2\repository
文件夹下的本地存储库。您应该能够在那里找到 org.apache.httpcomponents 。You built your jar correctly but when you are running the code you need to add the classpath to the jar containing
org.apache.httpcomponents
. If you want to package it as one jar then use the maven assembly pluginjar-with-dependencies
.By the way in case you did not know this, maven by convention downloads the repository jar files to the local repository under your
.m2\repository
folder. You should be able to locateorg.apache.httpcomponents
there.