java - 从终端运行时出现 Maven MultiModule 项目错误

标签 java intellij-idea maven-3 multi-module

我正在为神经网络创建客户端和服务器包装器。客户端读取图像并将其发送到运行模型的服务器,并将结果返回给客户端。 为此,我创建了一个多模块项目。当我从 IDE (intellij idea) 运行客户端和服务器时,一切正常。但是,当我使用 Maven 构建然后从终端运行时,我收到错误。

我的主/父 pom.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>gr.enomix</groupId>
<artifactId>imageClassificationWrapper</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
    <module>Server</module>
    <module>Client</module>
</modules>

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

<dependencies>

    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.0</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>2.0.5</version>
        <scope>compile</scope>
    </dependency>

</dependencies>

</project>

我的服务器 pom.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
    <artifactId>imageClassificationWrapper</artifactId>
    <groupId>gr.enomix</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>gr.enomix</groupId>
<artifactId>Server</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

</project>

我的客户端 pom.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
    <artifactId>imageClassificationWrapper</artifactId>
    <groupId>gr.enomix</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>gr.enomix</groupId>
<artifactId>Client</artifactId>
<packaging>jar</packaging>
<version>1.0</version>

</project>

我希望父 pom.xml 中的所有库都继承到子客户端和服务器。 然后我从终端运行 mvn 干净安装 并且项目成功构建,没有任何错误。

最后我执行

   java -cp target/Server-1.0.jar RunServer

运行服务器并

   java -cp target/Client-1.0.jar RunClient

运行客户端但出现错误

Exception in thread "main" java.lang.NoClassDefFoundError:     org/json/simple/JSONObject
    at ImageHttpClient.sendImage(ImageHttpClient.java:78)
    at RunClient.main(RunClient.java:11)

来自客户端和服务器。

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: org/apache/commons/io/IOUtils
    at     Handlers.ConnectionHandler.readAndSaveImage(ConnectionHandler.java:39)
    at Handlers.ConnectionHandler.run(ConnectionHandler.java:25)
    at java.lang.Thread.run(Thread.java:745)

Maven 依赖项未构建在类路径中??? 难道我做错了什么?? 请帮助我,这两天我的头都碎了......

最佳答案

您的依赖项未包含在 .jar 文件中,因此无法找到它们。因此出现 NoClassDefFoundError

您要做的就是将依赖项包含在您的 jar 中,即构建一个所谓的“fat jar”。

我不会发布如何做到这一点,因为 stackoverflow 上已经有很多帖子了。据我所知,您已经在评论中发表了一些内容。

编辑:要测试您的依赖项是否已包含,您可以使用 7zip 打开生成的 .jar 文件。

关于java - 从终端运行时出现 Maven MultiModule 项目错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47473913/

相关文章:

java - Maven 可执行 jar 打包 vs Maven exec with Jackson 不工作

java - 为什么 Maven 版本会跳过我的子模块

java - Eclipse RCP 插件中的多个 slf4j 绑定(bind)

java - 当您无法控制 RestTemplate 何时构建时,如何模拟 Spring 中 Rest 调用的响应

java - System.out.println 不在控制台中显示文本(IntelliJ)

scala - Intellij 似乎忽略了我在 Scala 项目中的 JUnit 测试

java - 如何在JAVA中更改firefox配置文件首选项字段,例如 “security.insecure_field_warning.contextual.enabled”

java - ANDROID - 如何使用 httpurlconnection 从 android 中的 Web 服务获得成功返回?

debugging - IntelliJ XSLT 调试器未命中断点

java - 如何检测激活了哪些 Maven 配置文件?