0

I've written a little Java command line tool, that I want to run by (for example)

java -jar myJarFile.jar de.my.path.MainClass arg0 arg1 arg2

For building the jar-File and managing the depedencies I am using Maven. Now I found out, that Maven does not include the depedencies of the POM-File in the Jar-File. So, when I run my jar-file, I get ClassNotFoundExceptions.. But to execute my jar-file I need those libraries in the classpath.

How can I manage this with Maven?

Thank you for your help!!

mrbela
  • 4,477
  • 9
  • 44
  • 79

2 Answers2

3

Put this maven assembly plugin in your pom.xml:

    <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>de.my.path.MainClass</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
Héctor
  • 24,444
  • 35
  • 132
  • 243
2

There are multiple options.

  • maven app assembly plugin

    This package all dependent jars and also creates linux/ windows scripts to run your application. See Here for more information on how to use it.

  • Maven assembly plugin

    It creates single executable jar including all dependencies. More here. However i found this problematic with some third party jars(spring) where there are files with same names.

Community
  • 1
  • 1
Adi
  • 2,364
  • 1
  • 17
  • 23