简体   繁体   English

从终端运行时,Maven MultiModule项目错误

[英]Maven MultiModule Project Errors When Run from terminal

I am creating a client and a server wrapper for a neural network. 我正在为神经网络创建客户端和服务器包装。 The client reads and sends an image to the server who runs the models and responds the results back to client. 客户端读取图像并将其发送到运行模型的服务器,然后将结果返回给客户端。 I have created a multimodule project for this reason. 因此,我创建了一个多模块项目。 When i run client and server from IDE (intellij idea) everything works ok. 当我从IDE(智能想法)运行客户端和服务器时,一切正常。 However when i use maven to build and then run from terminal i get errors. 但是,当我使用Maven构建并从终端运行时,出现错误。

My main/parent pom.xml is the following: 我的主要/父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>

My Server pom.xml is: 我的服务器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>

And my client pom.xml is: 我的客户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>

I want all libraries from parent pom.xml to be inherited to both children client and server. 我希望将父pom.xml中的所有库都继承到子客户端和服务器。 Then i run from terminal mvn clean install and the project successfully builds without any error. 然后,我从终端mvn clean install运行,该项目成功构建,没有任何错误。

Then finally i execute 然后最后我执行

   java -cp target/Server-1.0.jar RunServer

to run the server and 运行服务器并

   java -cp target/Client-1.0.jar RunClient

to run the client but i get errors 运行客户端,但出现错误

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

from both client and server. 来自客户端和服务器。

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 dependencies are not build in class path??? Maven依赖项不在类路径中构建??? Am i doing something wrong?? 难道我做错了什么?? Please Help i am breaking my head two days now... 请帮助我现在两天断头...

Your dependencies are not included in the .jar file and therefore they cannot be found. 您的依赖项未包含在.jar文件中,因此无法找到它们。 Hence the NoClassDefFoundError 因此, NoClassDefFoundError

What you have to do is to include the dependencies in your jar, ie build a so called "fat jar". 您要做的就是将依赖项包含在jar中,即构建一个所谓的“胖jar”。

I will not post how to do that, since there are already a lot of posts on stackoverflow. 我不会发布如何执行此操作,因为关于stackoverflow的帖子已经很多。 As I see you already have some in the comments. 如我所见,您已经在评论中了。

EDIT: To test if your dependencies have been included, you can open the generated .jar file using 7zip. 编辑:要测试是否已包含依赖项,可以使用7zip打开生成的.jar文件。

I think you should use dependencyManagement tag inside parent pom in order to let child poms inherit dependencies. 我认为您应该在父pom内使用dependencyManagement标记,以便让子pom继承依赖关系。

Check this link 检查此链接

differences between dependencymanagement and dependencies in maven Maven中依赖管理和依赖之间的区别

When you develop an standalone Java application, your dependencies are all being used only for the compilation phase. 在开发独立的Java应用程序时,所有依赖项仅用于编译阶段。 When you want to run your actual code, Java virtual machine needs to have the address of given dependencies to run your code. 当您要运行实际代码时,Java虚拟机需要具有给定依赖项的地址才能运行您的代码。 In order to do that, you've to tell the Java virtual machine explicitly where to find the libraries. 为此,您必须明确告诉Java虚拟机在哪里可以找到这些库。 You can add all libraries to your -cp option like this: 您可以将所有库添加到-cp选项中,如下所示:

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

in which {your-maven-repository-folder} normally is in your user's home folder. 其中{your-maven-repository-folder}通常位于用户的主文件夹中。 For example in linux it is /home/mixtou/.m2/repository . 例如,在Linux中,它是/home/mixtou/.m2/repository

You need to specify all your dependencies separating them with : in linux and ; 您需要指定所有依赖项,以linux和;分隔它们: in windows. 在Windows中。

As you're already using IntelliJ, one simpler solution could be to right click on your RunClient or RunServer class and select Run . 因为您已经在使用IntelliJ,所以一个更简单的解决方案是右键单击RunClientRunServer类,然后选择Run By doing so IntelliJ will try to run your project in the Run panel and if you check the very first line in the run output, you should see the exact command that IntelliJ is using to run your code which includes all necessary libraries. 这样,IntelliJ将尝试在“ Run panel运行您的项目,如果您在运行输出中检查了第一行,则应该看到IntelliJ用于运行代码的确切命令,其中包括所有必需的库。

Remember that in an standalone Java applications, no dependency would be included into your jar file. 请记住,在独立的Java应用程序中,jar文件中不会包含任何依赖项。 Such an option is only available in applications that are going to be run on an application server as war or ear file. 此选项仅在将以warear文件形式在应用程序服务器上运行的应用程序中可用。 Of course you can use some maven plugins that merge all your libraries into your jar file (which is normally known as fat jar) and then you would not need to specify anything in classpath, but then your jar file may become too big (depends on the number of libraries you use) and so updating that would be more cumbersome. 当然,您可以使用一些Maven插件将所有库合并到您的jar文件(通常称为fat jar)中,然后您无需在类路径中指定任何内容,但是jar文件可能会变得太大(取决于(您使用的库的数量),因此进行更新会比较麻烦。

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

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