Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executable jar with dependency using maven without unpacking jars but include classpath

Tags:

java

maven

jar

I probably need something like this question

Maven build assembly with dependencies

or maven assembly create jar with dependency and class path

How do I execute the executable jar ?

java -jar at commandline gives me Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger

I have executable.jar created in my target directory and lib directory with all dependency jar under /lib directoty.

This is the snippet of my pom.xml.What should I be changing?

</dependencies> 
 <dependency>
<groupId><gId></groupId>
<artifactId><aId></artifactId>
<version><v></version>
</dependency>
<dependency>
<groupId>gI2</groupId>
<artifactId>aI2</artifactId>
<version>v</version>
</dependency>
</dependencies>
<build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.proj.app</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
like image 937
S__R Avatar asked Jan 27 '26 03:01

S__R


1 Answers

Uses Maven Shade plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.6</version>
    <configuration>
        <createDependencyReducedPom>true</createDependencyReducedPom>
        <filters>
            <filter>
                <artifact>*:*</artifact>
                <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                </excludes>
            </filter>
        </filters>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass><Your main class here></mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

This will include all your dependencies in one jar and will automatically add the main class to your Manifest file.

Make sure you run the "package" goal.

like image 95
sathishvisa Avatar answered Jan 28 '26 15:01

sathishvisa