简体   繁体   English

尝试从命令行运行Maven项目时出错

[英]Errors trying to run a maven project from the Command Line

I am really struggling trying to get my maven project to run in the command line. 我真的很想让我的Maven项目在命令行中运行。 It runs how I want it to in eclipse, but using mvn package command in the terminal runs the unit tests that I wrote, but not the actual main source code application that I want it to run. 它可以在eclipse中运行我希望的运行方式,但是在终端中使用mvn package命令可以运行我编写的单元测试,但不能运行我希望它运行的实际主要源代码应用程序。

java -cp target/TestKata-0.0.1-SNAPSHOT.jar com.techelevator.Main

Returns this error: 返回此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: okhttp3/OkHttpClient
    at com.techelevator.Main.<clinit>(Main.java:12)
Caused by: java.lang.ClassNotFoundException: okhttp3.OkHttpClient
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

I assume I'm getting this error, because the dependencies are not included in the SNAPSHOT.jar file and it fails after it gets to the first line where a dependency is required?? 我假设我收到此错误,因为SNAPSHOT.jar文件中不包含依赖项,并且在到达需要依赖项的第一行后它失败了? Any help is greatly appreciated, I am very new to this stuff so I apologize if I'm doing something really stupid. 非常感谢您的帮助,对于这些东西我还是很陌生,因此如果我做的真的很愚​​蠢,我深表歉意。

EDIT: Here is my pom.xml 编辑:这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.techelevator</groupId>
    <artifactId>TestKata</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium</artifactId>
            <version>2.0rc2</version>
            <type>pom</type>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.3</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehouse.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.4.0</version>
                <configuration>
                    <mainClass>com.techelevator.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

you must package your dependencies into your jar via maven-assemble-plugin.the jar-with-dependnecies descriptor find in maven 您必须通过maven-assemble-plugin将依赖项打包到jar中。在maven中找到jar-with-dependnecies描述符

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

you can add executions when you run mvn package command,that will output a jar filename like *-with-jar-dependencies.jar and then run that jar file instead via java ,eg: java -cp target/TestKata-0.0.1-SNAPSHOT-with-jar-dependencies.jar com.techelevator.Main 您可以在运行mvn package命令时添加执行,这将输出一个jar文件名,例如*-with-jar-dependencies.jar ,然后通过java运行该jar文件,例如: java -cp target/TestKata-0.0.1-SNAPSHOT-with-jar-dependencies.jar com.techelevator.Main

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM