如何从 ubuntu 上的命令行使用 maven 运行 Java 应用程序? [英] How do I run a java application with maven from command line on ubuntu?
问题描述
我正在尝试从 ubuntu 上的命令行使用 maven 运行这个 java 应用程序OpenJDK 13
I'm trying to run this java application with maven from command line on ubuntu with OpenJDK 13
openjdk version "13.0.2" 2020-01-14
OpenJDK Runtime Environment AdoptOpenJDK (build 13.0.2+8)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 13.0.2+8, mixed mode, sharing)
该项目是使用 Intellij Idea 创建的.
The project is created with Intellij Idea.
我想我已经使用这些命令成功构建了
I guess I've made the build successfully with these commands
git clone https://github.com/danvega/httpclient-tutorial.git
cd httpclient-tutorial
mvn package
但是,我不知道如何从命令行运行应用程序.
However, I don't know how to run the application from command line.
我试过这些命令
cd target/classes
java dev.danvega.Application
并得到这个错误
Error: Unable to initialize main class dev.danvega.Application
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/type/TypeReference
我错过了什么?
推荐答案
在这里,你有一个 maven 项目,它对 jackson-databind
有一个依赖,而后者又会有更多的依赖,即 jackson-core
和 jackson-annotations
.
Here, you have maven project which has one dependency on jackson-databind
which in turn will have some more dependencies i.e jackson-core
and jackson-annotations
.
来自这些依赖项的类未捆绑在您的应用程序 jar 中,因此您不能直接使用 java
命令从项目中直接运行 Application
主类,您需要指定java类路径上的依赖类,以便java可以加载程序的这些依赖类.
Classes from these dependencies are not bundled in your application jar, so you cannot just run the Application
main class from your project directly using java
command, you need to specify the dependent classes on java classpath so that java can load these dependent classes of your program.
由于是maven项目,这些依赖的jars
会被拉到maven
默认目录(.m2
)到你的home路径正如您所提到的,您使用的 ubuntu 将是 /home/
,例如您登录时使用的用户名是 singularli
那么您的home路径必须是/home/singularli
,你也可以用echo $HOME
命令查看.
Since, it is a maven project, these dependent jars
will be pulled into maven
default directory (.m2
) into your's home path and as you mentioned, you are using ubuntu that will be /home/<your username>/
, For example your username which you are logged in with is singularli
then your home path must be /home/singularli
, you can also check it with echo $HOME
command.
所以,你会在你的家 /home/singularli/.m2/repository
中找到存储所有 jar 的 maven 文件夹,现在你会在这里找到类似 jackson-databind
、jackson-core
(这些在子目录中会很小,因为它根据包名保存,下面给出的命令示例会让你对它有更多的了解).
So, you would find the maven folder, which stores all the jar(s), into your home /home/singularli/.m2/repository
, now here you would find jars like jackson-databind
, jackson-core
(these will be little inside subdirectories, as it keeps according to the package name, given below command example will give you more idea about it).
最后,一旦你找到这些 jars
,你需要使用 -cp
标志指定类路径,并将这些 jars 包含在你的应用程序 jar 中,看起来像下面给出:
At last, once you find these jars
, you would need to specify the classpath using -cp
flag and include these jars with your application jar which would look like as given below:
java -cp "target/httpclient-tutorial-1.0-SNAPSHOT.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.11.4/jackson-core-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.11.4/jackson-databind-2.11.4.jar:/home/singularli/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.11.4/jackson-annotations-2.11.4.jar" dev.danvega.Application
它的工作方式应该与您在问题中提到的那个视频中显示的一样.
It should work the same way as shown in that video, you referred in your question.
请注意,您可能有不同的版本,即 com/fasterxml/jackson/core/jackson-annotations/2.11.4,我以 2.11.4 为例,您可以查看此项目中的版本并包含也就是说,如果存在不同的版本并且您包含了其中的任何一个,则可能会导致一些问题,因为该项目中使用的某些功能可能不会出现在该版本中
这篇关于如何从 ubuntu 上的命令行使用 maven 运行 Java 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!