Complete Guide to Creating Runnable JAR Files with Gradle

Nov 21, 2025 · Programming · 11 views · 7.8

Keywords: Gradle | Runnable JAR | Java Build

Abstract: This article provides a comprehensive guide to creating runnable JAR files using Gradle build tool, focusing on the core technique of configuring Main-Class in manifest attributes. It compares alternative approaches including the application plugin and fat JAR solutions, based on high-scoring Stack Overflow answers and practical case studies. The content helps developers smoothly transition from IDEs like Eclipse to Gradle build environments with complete implementation examples.

Technical Principles of Building Runnable JAR Files with Gradle

In Java application development, runnable JAR files serve as a common deployment format. Unlike export functionalities in IDEs like Eclipse, Gradle builds require explicit configuration of the Main-Class attribute in the manifest file. According to high-scoring Stack Overflow answers, the core implementation involves configuring the manifest block of the jar task to specify the main class.

Basic Configuration Method

The simplest approach to create a runnable JAR is to directly configure the jar task in the build.gradle file. The following code example demonstrates how to set the Main-Class attribute:

jar {
    manifest {
        attributes 'Main-Class': 'com.foo.bar.MainClass'
    }
}

This method's advantage lies in its straightforwardness, requiring no additional plugin dependencies. Developers need only replace com.foo.bar.MainClass with the actual fully qualified name of their main class.

Integration with Application Plugin

If the application plugin is already used in the project, its configuration can be reused to avoid code duplication. This integrated approach enhances configuration maintainability:

application {
    mainClass = 'com.foo.bar.MainClass'
}

jar {
    manifest {
        attributes 'Main-Class': application.mainClass
    }
}

By referencing the application.mainClass property, configuration consistency is ensured, proving particularly valuable in multi-module projects.

Handling Classpath Dependencies

In real-world projects, applications typically depend on external libraries. While basic manifest configuration is simple, it has limitations when handling dependencies. As mentioned in reference answers, adding Class-Path entries might be necessary:

jar {
    manifest {
        attributes 'Main-Class': 'com.example.MainApp'
        attributes 'Class-Path': configurations.runtimeClasspath.files.collect { 'libs/' + it.name }.join(' ')
    }
}

This configuration assumes dependent JAR files are located in a libs directory, requiring matching directory structure during actual deployment.

Comparison of Alternative Approaches

Beyond direct manifest configuration, the Gradle ecosystem offers various alternatives. The application plugin, while not directly generating runnable JARs, provides run tasks and complete application distribution capabilities. For scenarios requiring inclusion of all dependencies, fat JAR solutions are more appropriate, with commonly used plugins including Shadow, One-Jar, and Spring Boot Gradle Plugin.

Practical Recommendations and Migration Strategies

When migrating from Eclipse export functionality to Gradle, starting with basic manifest configuration is recommended to ensure core functionality works correctly. For complex projects, gradually introduce application plugin or fat JAR solutions. The Spring Boot plugin mentioned in reference articles, while powerful, primarily suits Spring Boot projects, with generic Java applications still relying on the fundamental configuration methods described above.

Conclusion

Creating runnable JAR files is a fundamental requirement for building Java applications with Gradle. Through proper configuration of manifest attributes, developers can achieve results equivalent to IDE export functionalities. The methods introduced in this article, based on community-validated best practices, provide scalable solutions for projects of varying complexity.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.