【问题标题】:Maven MultiModule Project Errors When Run from terminal从终端运行时的 Maven MultiModule 项目错误
【发布时间】:2018-05-08 12:09:24
【问题描述】:

我正在为神经网络创建一个客户端和一个服务器包装器。客户端读取图像并将其发送到运行模型并将结果返回给客户端的服务器。 为此,我创建了一个多模块项目。当我从 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 依赖没有建立在类路径中??? 难道我做错了什么?? 请帮帮我,我这两天头都破了……

【问题讨论】:

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


【解决方案1】:

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

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

我不会发布如何做到这一点,因为在 stackoverflow 上已经有很多帖子了。如我所见,您已经在 cmets 中有一些。

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

【讨论】:

【解决方案2】:

我认为您应该在父 pom 中使用 dependencyManagement 标签,以便让子 pom 继承依赖项。

查看此链接

differences between dependencymanagement and dependencies in maven

【讨论】:

  • dependencyManagement 不会导致子模块中的依赖关系的继承。无论如何,这不是问题。问题在于构建一个胖罐子。
  • 你是对的,我很抱歉误导。我想你还是需要为孩子添加依赖项
  • 问题正是DeiAndrei和andgalf提到的。在 DeiAndrei 的回复中查看我的评论以获取解决方案。
【解决方案3】:

当您开发一个独立的 Java 应用程序时,您的所有依赖项都仅用于编译阶段。当您想要运行您的实际代码时,Java 虚拟机需要具有给定依赖项的地址才能运行您的代码。为此,您必须明确告诉 Java 虚拟机在哪里可以找到这些库。您可以像这样将所有库添加到您的 -cp 选项中:

java -cp {your-maven-repository-folder}/org/apache/common/commons-io/commons-io-1.3.2.jar:target/Client-1.0.jar RunClient

其中 {your-maven-repository-folder} 通常位于用户的主文件夹中。例如在 linux 中是/home/mixtou/.m2/repository

您需要指定所有您的依赖项,在 linux 中使用 : 和在 Windows 中使用 ; 分隔它们。

由于您已经在使用 IntelliJ,一种更简单的解决方案可能是右键单击您的 RunClientRunServer 类并选择 Run。通过这样做,IntelliJ 将尝试在 Run panel 中运行您的项目,如果您检查运行输出中的第一行,您应该会看到 IntelliJ 用于运行您的代码的确切命令,其中包括所有必要的库。

请记住,在独立的 Java 应用程序中,您的 jar 文件中不会包含任何依赖项。这样的选项仅在将在应用程序服务器上作为warear 文件运行的应用程序中可用。当然,您可以使用一些 maven 插件将所有库合并到您的 jar 文件(通常称为 fat jar)中,然后您不需要在类路径中指定任何内容,但是您的 jar 文件可能会变得太大(取决于您使用的库的数量),因此更新会更加麻烦。

【讨论】:

    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多