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

时间:2017-03-19 02:41:36

标签: java eclipse maven command-line

我真的在努力让我的maven项目在命令行中运行。它在eclipse中运行我想要它的运行方式,但在终端中使用mvn package命令运行我编写的单元测试,但不运行我希望它运行的实际主要源代码应用程序。

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

返回此错误:

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

我假设我收到此错误,因为SNAPSHOT.jar文件中没有包含依赖项,并且它到达需要依赖项的第一行后会失败?非常感谢任何帮助,我对这些东西都很陌生,所以如果我做的事情非常愚蠢我会道歉。

编辑: 这是我的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>

1 个答案:

答案 0 :(得分:2)

你必须通过maven-assemble-plugin. jar-with-dependnecies中的<build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> 描述符将你的依赖项打包到你的jar中

mvn package

您可以在运行*-with-jar-dependencies.jar命令时添加执行,这将输出类似java的jar文件名,然后通过java -cp target/TestKata-0.0.1-SNAPSHOT-with-jar-dependencies.jar com.techelevator.Main运行该jar文件,例如:<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>

uint16_t big = 0xBEEF;

big = (big & 0xFF00) | 0x42;
assert(big == 0xBE42);

big = (big & 0x00FF) | 0x86 << 8;
assert(big == 0x8642);