[英]How do I package and run a simple command-line application with dependencies using maven?
[英]How do I run a java application with maven from command line on ubuntu?
我正在嘗試使用 OpenJDK 13 從 ubuntu 上的命令行使用 maven 運行此java 應用程序
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 創建的。
我想我已經使用這些命令成功構建了
git clone https://github.com/danvega/httpclient-tutorial.git
cd httpclient-tutorial
mvn package
但是,我不知道如何從命令行運行應用程序。
我試過這些命令
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
我錯過了什么?
You started Java without specifying where the Maven dependencies can be found, which is called CLASSPATH (and since Java 9 also MODULEPATH if you use the Java Module System), similar how *.dll files can be found in the PATH on windows or LD_LIBRARY_PATH can用於 Unix 系統上的 *.so
請在您之前查看其他人的精彩回答和問題:
我個人更喜歡:
java -jar yourfat.jar
(or just click and it will start on Windows) 在這里,您有 maven 項目,該項目對 jackson-databind 有一個依賴項,而jackson-databind
又將具有更多依賴項,即jackson-core
和jackson-annotations
。
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 .
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您登錄的用戶名是singularli
,那么您的主路徑必須是/home/singularli
,您也可以使用echo $HOME
命令檢查它。
所以,你會發現 maven 文件夾,它存儲所有的 jar(s),到你的家/home/singularli/.m2/repository
,現在你會在這里找到 jars 像jackson-databind
, jackson-core
(這些會很少在子目錄中,因為它根據 package 名稱保留,下面給出的命令示例將讓您對此有更多了解)。
最后,一旦找到這些jars
,您需要使用-cp
標志指定類路徑並將這些 jars 包含在您的應用程序 jar 中,如下所示:
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
它的工作方式應該與您在問題中提到的視頻中顯示的方式相同。
請注意,您可能有不同的版本,即 com/fasterxml/jackson/core/jackson-annotations/2.11.4,我以 2.11.4 為例,您可以檢查此項目中的版本並包括,如果不同的版本在那里,你包括了他們中的任何一個,這可能會導致一些問題,因為該項目中使用的某些功能可能不存在於該版本中
The third-party dependency that contains com/fasterxml/jackson/core/type/TypeReference
(which you can find the pom.xml com.fasterxml.jackson.core:jackson-databind
is required at both compile time and runtime. If you run使用java
命令,您需要指定對類路徑的依賴關系。但是由於您使用的是 Maven,因此您可以使用exec-maven-plugin
為方便起見,它將在運行時處理類路徑:
mvn exec:java -Dexec.mainClass="dev.danvega.Application"
您還可以編譯然后在同一命令中運行:
mvn package exec:java -Dexec.mainClass="dev.danvega.Application"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.