0

I have a mvn built jar, which is working fine inside the target folder.

But if I copy the jar to different folder/machine.I get a class not found exception. I understand the jar is unable to access the dependencies. how can i fix this? my pom contains,

            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
            <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
            <goal>copy-dependencies</goal>
            </goals>
            <configuration>
            <configuration>
            <!-- exclude junit, we need runtime dependency only -->
            <includeScope>runtime</includeScope>
            <outputDirectory>${project.build.outputDirectory}/dependency/
            </outputDirectory>
            </configuration>
            </configuration>
            </execution>
            </executions>
            </plugin>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>

            <configuration>

            <archive>
            <manifest>
            <addClasspath>true</addClasspath>
            <mainClass>com.Main</mainClass>

            <classpathPrefix>dependency/</classpathPrefix>

            </manifest>
            </archive>
            </configuration>
            </plugin>

Thanks In Advance, Saranya

Saran
  • 69
  • 7

2 Answers2

0

Make a fat jar with dependencies using maven-assembly-plugin:

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <!-- get all project dependencies -->
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <!-- bind to the packaging phase -->
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>

The target folder would have additional jar-with-dependencies.jar; you should be able to use this anywhere.

Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
  • but my output jar has to be predefined name and should not include jar-with-dependencies.jar. Also when i include this plugin it says no main manifest – Saran Mar 07 '18 at 05:53
  • Change `descriptorRef` to whatever you wish. point is that maven will create two jars - one without including dependencies other with dependencies and you would like to have a separate name. Anyway, I do not understand, how jar name matters from execution perspective. – Gyanendra Dwivedi Mar 07 '18 at 05:57
  • So everywhere I seen that copy dependency plugin can also be used to have a executable jar. But for me, I can execute the jar only when I point inside target folder. – Saran Mar 07 '18 at 23:25
  • I tried maven assembly plugin with slight changes to your sample. But is there any way to get rid of the jar without dependencies or update its default name. jar with dependencies works inside and outside target folder. So jar without dependencies is of no use from my point of view. – Saran Mar 07 '18 at 23:57
  • 1
    Jar with dependency is just a way in certain circumstances, but should not be the way to make jar. Think about a big project having 1000s of dependent jar any club all together. Your Jar would become huge in size and loose its portability and many benefits of Java platform. Ideally you learn how to use a normal jar by setting classpath at runtime. Meanwhile, I do not think it is possible to get rid of files and rename jars, unless you use another plugin. – Gyanendra Dwivedi Mar 08 '18 at 18:49
0

I removed the maven jar and dependency plugins from my pom. Added

 <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.importer.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <finalName>xyz</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance 
      merges -->
                        <phase>package</phase> <!-- bind to the packaging phase 
       -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

This created a jar with dependencies which can be executed anywhere. Also with the specific jar name.

Saran
  • 69
  • 7