How to Build and Package a Java Application?

Understand how to compile and package your Java project into a deployable JAR file using Maven or Gradle.

📌 Why Do I Need to Build My Java Application?

Discloud does not compile Java projects automatically.

You must: ✔ Compile your code into .class files. ✔ Package everything into a single .jar file (including dependencies). ✔ Ensure the META-INF/MANIFEST.MF file correctly defines the Main-Class.


🔧 Choosing a Build Tool

Build Tool
Best For
Required Files

Maven

Large projects, dependency-heavy apps

pom.xml

Gradle

Modern projects with flexible build scripts

build.gradle or build.gradle.kts


📦 Building Your Java Application

Apache Maven is a widely used build automation tool that manages project builds, dependencies, and documentation using a Project Object Model (POM).​

1

Add the Maven Compiler Plugin.

Ensure your pom.xml includes the Maven Compiler Plugin to specify the Java version for compilation. This prevents compatibility issues when running the project.

pom.xml
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.9.9</version>
  <configuration>
    <source>17</source>
    <target>17</target>
  </configuration>
</plugin>
  • Set <source> and <target> to the Java version required for your project.

2

Specify the Main Class.

To make your application executable, you need to specify the entry point (main method). This is done using the Maven Shade Plugin, which also ensures that all dependencies are bundled inside a single JAR file.

pom.xml
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-shade-plugin</artifactId>
      <version>3.2.4</version>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <transformers>
              <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <!-- NOTE: Replace this with the correct path to your project's main class -->
                <mainClass>com.project.example.Main</mainClass>
              </transformer>
            </transformers>
          </configuration>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
  • Replace com.project.example.Main with the actual fully qualified name of your Main class.

  • Ensure the Main class contains a valid public static void main(String[] args) method.

3

Build the Project.

Run the following command to clean old builds and generate the new JAR files:

mvn clean package

After running mvn package, Maven will create two different JAR files inside the target/ directory:

1

Standard JAR (Original Project JAR).

📌 Filename: original-<artifactId>-<version>.jar 📌 Example: original-com.maven.discordbot-0.0.1-SNAPSHOT.jar 📌 Contents:

  • Only your project's compiled code.

  • Does NOT include dependencies.

2

Fat JAR (Shaded JAR with Dependencies).

📌 Filename: <artifactId>-<version>.jar 📌 Example: com.maven.discordbot-0.0.1-SNAPSHOT.jar 📌 Contents:

  • Your compiled application code.

  • All dependencies included (JDA, Apache Commons, etc.).

  • Manifest file (MANIFEST.MF) with the Main-Class entry.

Recommendation

Rename your JAR file to a simple name like app.jar to avoid issues with special characters.​

Last updated