发表于: 2021-11-05 21:36:16

2 988


使用Maven运行Java main的3种方式

一、从命令行运行

1、运行前先编译代码,exec:java不会自动编译代码,你需要手动执行mvn compile来完成编译。

  1. mvn compile  

2、编译完成后,执行exec运行main方法。

不需要传递参数:

  1. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main"  

需要传递参数:

  1. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.args="arg0 arg1 arg2"  

指定对classpath的运行时依赖:

  1. mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime  

 IDEA 中 将项目打包并上传到阿里服务器上 /task的文件夹下

先编译

mvn copile

[WARNING] 

[WARNING] Some problems were encountered while building the effective settings

[WARNING] expected START_TAG or END_TAG not TEXT (position: TEXT seen ...<url>https://maven.aliyun.com/repository/public</url>;\r\n  <m... @157:5)  @ /usr/lib/maven/conf/settings.xml, line 157, column 5

[WARNING] 

[INFO] Scanning for projects...

[INFO] ------------------------------------------------------------------------

[INFO] BUILD FAILURE

[INFO] ------------------------------------------------------------------------

[INFO] Total time:  0.050 s

[INFO] Finished at: 2021-11-06T10:19:32+08:00

[INFO] ------------------------------------------------------------------------

[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/task). Please verify you invoked Maven from the correct directory. -> [Help 1]

[ERROR] 

[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.

[ERROR] Re-run Maven using the -X switch to enable full debug logging.

[ERROR] 

[ERROR] For more information about the errors and possible solutions, please read the following articles:

[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException

运行报错查找资料并分析原因是没有pom.xml 文件,把pom.xml 文件也放在同一目录下编译成功

[WARNING] 

[INFO] Scanning for projects...

[INFO] 

[INFO] ------------------------< org.example:TestTask >------------------------

[INFO] Building TestTask 1.2-SNAPSHOT

[INFO] --------------------------------[ jar ]---------------------------------

[INFO] 

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ TestTask ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory /task/src/main/resources

[INFO] 

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ TestTask ---

[INFO] No sources to compile

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time:  0.443 s

[INFO] Finished at: 2021-11-06T10:21:37+08:00

[INFO] ----------------------------------------


执行main方法

mvn exec:java -Dexec.mainCalss=com.jnshu.task.HelloWorld

又报错:An exception occured while executing the Java class. com.jnshu.task.HelloWorld -> [Help 1]
主要提示 找不到com.jnshu.task.HelloWorld 这个类
然后就是各种查资料,最后明白是需要src这个文件一起上传来才能运行

mvn exec:java -Dexec.mainCalss=com.jnshu.task.HelloWorld

再运行还是报错An exception occured while executing the Java class. com.jnshu.task.HelloWorld 

自己想应该是没有编译的问题,再执行mvn compile  结果BUILD SUCCESS

 用ls 命令查看当前文件夹 发现多处来target文件了

执行mvn exec:java -Dexec.mainCalss=com.jnshu.task.HelloWorld

[WARNING] 

[INFO] Scanning for projects...

[INFO] 

[INFO] ------------------------< org.example:TestTask >------------------------

[INFO] Building TestTask 1.2-SNAPSHOT

[INFO] --------------------------------[ jar ]---------------------------------

[INFO] 

[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ TestTask ---

Hello World

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time:  0.310 s

我的main中写的是

System.out.println("Hello World");






返回列表 返回列表
评论

    分享到