简体   繁体   English

通过命令行运行Java项目(具有Maven依赖项)

[英]Running Java project through command line (with Maven dependencies)

I want to run my working Java project through my terminal (mac). 我想通过终端(mac)运行可运行的Java项目。 It uses Maven to pull in required dependencies. 它使用Maven引入所需的依赖关系。 Any help would be very much appreciated. 任何帮助将不胜感激。

First I ran the commend: mvn clean install 首先,我运行了该命令: mvn clean install

Next: mvn dependency:copy-dependencies 下一页: mvn dependency:copy-dependencies

Finally: cd target/ java -cp MyProject-1.0-SNAPSHOT.jar:dependency Main 最后: cd target/ java -cp MyProject-1.0-SNAPSHOT.jar:dependency Main

Unfortunately the following error is thrown (clearly showing the dependencies are not being used correctly): 不幸的是,抛出了以下错误(清楚地显示依赖关系未正确使用):

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser at Main$Quote.quoteMachine(Hi.java:21) at Main.main(Hi.java:12) Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser ... Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser at Main$Quote.quoteMachine(Hi.java:21) at Main.main(Hi.java:12) Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser ...

I managed to solve the problem by adding this to my POM file: 通过将其添加到我的POM文件中,我设法解决了这个问题:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>main</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

After this I simply ran the following commands in my terminal: 之后,我只需在终端中运行以下命令:

mvn clean install

And then: 接着:

mvn exec:java

您需要在类路径中包含json-simple-.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法通过Java命令从命令行执行Maven项目 - Unable to execute Maven project from command line through java command 从命令行运行具有依赖项的 maven 项目 - run maven project with dependencies from command line 如何通过命令行编译和运行Java Maven项目? - How to compile and run java maven project through command line? 如何从命令行编译Maven项目与所有依赖项? - How to compile Maven project from command line with all dependencies? 使用命令行运行Java Maven项目 - run java maven project with command line 使用Linux命令行运行Java Maven项目 - Run Java Maven project with Linux command line Maven - 如何通过命令行为 java 项目创建 pom.xml - Maven - How to create a pom.xml for java project through command line Maven Multi-Module-Project可在IntelliJ中运行,但通过命令行运行时显示“ java.lang.NoClassDefFoundError” - Maven Multi-Module-Project works in IntelliJ but running via command line says “java.lang.NoClassDefFoundError” 从命令行(Eclipse外)运行Java应用程序(Maven项目)时出错 - Errors when running Java Application (maven project) from command line (outside eclipse) 如何解决在eclipse中通过maven导入的java项目中的依赖项? - How to resolve the dependencies in the java project which is imported through maven in eclipse?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM