Combining multiple JAR files into a single JAR file can be useful in various scenarios, such as creating a self-contained executable JAR or consolidating libraries. Here's a clean way to combine multiple JAR files into one:
Using Gradle or Maven (Build Tools): If you are using Gradle or Maven as your build tool, you can use the "fat JAR" or "uber JAR" approach to combine multiple JAR files into a single JAR. Here's how to do it with both build tools:
Gradle:
In your build.gradle
file, add a plugin to handle fat JAR creation. For example, you can use the "Shadow" plugin:
plugins { id 'com.github.johnrengelman.shadow' version '7.0.0' }
Configure the Shadow plugin to build your fat JAR:
shadowJar { mergeServiceFiles() }
Run the ./gradlew shadowJar
command to build the fat JAR.
Maven:
In your pom.xml
file, add the maven-assembly-plugin
to handle creating an uber JAR:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
Run the mvn package
command to build the uber JAR.
Using Command Line (Jar Command):
You can also combine JAR files using the jar
command-line tool that comes with the Java Development Kit (JDK). This approach is more manual but provides fine-grained control.
Create a directory where you want to combine the JAR files.
Copy all the JAR files you want to combine into this directory.
Open a terminal or command prompt and navigate to the directory containing the JAR files.
Use the jar
command to create a new JAR file and add the contents of the other JAR files to it. For example:
jar cvf combined.jar *
This command creates a new JAR file named combined.jar
and adds all the files from the current directory to it.
If you need to specify a specific main class or other manifest attributes, you can create a manifest file (manifest.mf
) and include it when creating the JAR:
jar cvfm combined.jar manifest.mf *
The Gradle or Maven approach is recommended for most projects because it automates the process and handles dependencies. However, if you have a specific use case or need fine-grained control, you can use the command-line jar
tool.
area reverse-engineering text-files userscripts mkmapview nuxt.js tiff dfsort svg-filters pcm