0

I created a Maven project on IntelliJ and it runs correctly. Now, I would like to run my project with Docker and without IntelliJ.

For this, I use this command : docker run -it --rm --name my-maven-project -v /var/www/html/Recommandation/:/usr/src/mymaven -p 88:88 -w /usr/src/mymaven maven:latest /bin/bash -c "mvn clean install && java -jar target/Recommandation-1.0-SNAPSHOT.jar org.exemple.demo.App"

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  42.037 s
[INFO] Finished at: 2020-07-01T11:33:43Z
[INFO] ------------------------------------------------------------------------
Error: Unable to initialize main class org.exemple.demo.App
Caused by: java.lang.NoClassDefFoundError: io/vertx/core/Verticle

But there is a dependency resolution issue. Any idea ?

Here's my 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>org.exemple.demo</groupId>
    <artifactId>Recommandation</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <defaultGoal>install</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.exemple.demo.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <packaging>jar</packaging>

    <name>Recommandation</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>7</maven.compiler.source>
        <maven.compiler.target>7</maven.compiler.target>
    </properties>

    <dependencies>
    ...
    </dependencies>
</project>
  • 1
    Does this answer your question? [How can I create an executable JAR with dependencies using Maven?](https://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven) – Joe Jul 01 '20 at 14:03
  • This is a standalone project or you are referring to another project within your project? – Tarun Lalwani Jul 01 '20 at 14:04
  • No because I'm able to build my project. Here I just want to run it with Docker. (I'm also able to run it with IntelliJ). @TarunLalwani It's a standalone project but there is a lot of dependancies. I will put my full pom.xml. – Gaël GRZELAK Jul 01 '20 at 14:10
  • I can't, to much code for stackoverflow... :-/ – Gaël GRZELAK Jul 01 '20 at 14:22
  • I would recommend you to see this article as well, https://phauer.com/2019/no-fat-jar-in-docker-image/ – Tarun Lalwani Jul 01 '20 at 14:53

1 Answers1

1

As I see you did not include the dependencies into your jar.

You need to build an executable jar for that.

IntelliJ allows you to run jars by pulling the dependencies in the background, but if you want to run a jar from command line it needs to include its dependencies (or you need to reference them from an external directory).

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Ok I will try to follow the explanation of Joe's link and I put RESOLVE if I manage to launch my project. thank you for the explanations. – Gaël GRZELAK Jul 01 '20 at 14:52
  • It's working. Indeed, it seems easier to have the dependency references in the executable jar file. Thanks to @Joe for his reference. – Gaël GRZELAK Jul 02 '20 at 07:46