Home | License | FAQ | Contact | Smart-API

Last Updated: October 20, 2022

How to create a runnable JAR file

Pre-Requisite:

Create a maven project.

Steps:

1.Go to the pom file. as shown in Figure 1.

 

project-screen1.png
Figure 1. prjoect
2.Add the plugin to the pom file. as shown in Figure 2.

 

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jalalkiswani</groupId>
	<artifactId>runnable-jar-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<build>
		<plugins>
			<plugin>
				<!-- Build an executable JAR -->
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
						<configuration>
							<classifier>full</classifier>
							<mainClass>com.app.Main</mainClass>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>
pomfile-screen2.png
Figure 2. pom file
change <mainClass>com.path.to.YourMainClass</mainClass> with path to your Main Class.
3.Update maven project. as shown in Figure 3.

Right-click on the project → Maven → Update Project Or Alt + F5, then Click OK.

update-maven-screen3.png
Figure 3. update maven project
4.Run the project as maven build. as shown in Figure 4.

Right-click on the Run As → Maven build ,Click on the package, then OK.

run-build-screen4.png
Figure 4. create runnable jar file

Wait for the build to finish. as shown in Figure 5.

project-build-success-screen5.png
Figure 5. create runnable jar file
5.Navigate to target file in your project location. as shown in Figure 6.

 

target-file-screen6.png
Figure 6. navigate to project

You should see a jar file created as shown in Figure 6.

6.Run with command line. as shown in Figure 7.

open CMD or Terminal and type :

cd [Path]

java -jar [YourJarName].jar
Replace [Path] with your project path and [YourJarName.jar] with your jar file name.
run-jar-file-screen7.png
Figure 7. run with command

You have created a runnable jar file.

Runnable Jar using Standard Maven Plugin

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.jalalkiswani</groupId>
	<artifactId>runnable-jar-example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.3</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>com.app.Main</mainClass>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Home | License | FAQ | Contact | Smart-API