Skip to main content

Overview

This guide demonstrates how to create a portable, executable JAR file for the HSQLDB enterprise application. The JAR packaging process enables easy distribution and deployment across different systems without requiring manual compilation.

What is CrearJar.java?

CrearJar.java is a specialized utility that automates the entire JAR creation process, including:
  • Compiling the source code
  • Generating the manifest file
  • Creating configuration files
  • Packaging everything into an executable JAR
  • Cleaning up temporary files

JAR Creation Process

Complete Source Code

import java.io.*;
import java.nio.file.*;
import java.util.*;

public class CrearJar {
    public static void main(String[] args) throws IOException {
        System.out.println("=== GENERADOR DE JAR PORTABLE ===");
        
        // Step 1: Compile the source file
        System.out.println("1. Compilando DB_EnterpriseHSQLDB...");
        ProcessBuilder compilar = new ProcessBuilder(
            "javac", "-cp", "hsqldb-2.7.2.jar", "DB_EnterpriseHSQLDB.java"
        );
        Process procesoCompilar = compilar.start();
        try {
            procesoCompilar.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // Step 2: Create manifest file
        System.out.println("2. Creando MANIFEST.MF...");
        String manifest = 
            "Manifest-Version: 1.0\n" +
            "Main-Class: DB_EnterpriseHSQLDB\n" +
            "Class-Path: hsqldb-2.7.2.jar\n";
        
        Files.write(Paths.get("MANIFEST.MF"), manifest.getBytes());
        
        // Step 3: Create configuration file
        System.out.println("3. Creando config.ini...");
        String config = 
            "#Configuración HSQLDB\n" +
            "database.path=./db_empresa\n";
        
        if (!Files.exists(Paths.get("config.ini"))) {
            Files.write(Paths.get("config.ini"), config.getBytes());
        }
        
        // Step 4: Generate the JAR file
        System.out.println("4. Generando JAR...");
        ProcessBuilder crearJar = new ProcessBuilder(
            "jar", "cfm", "EmpresaHSQLDB.jar", "MANIFEST.MF", 
            "DB_EnterpriseHSQLDB.class", "config.ini"
        );
        Process procesoJar = crearJar.start();
        try {
            procesoJar.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        // Step 5: Clean up temporary files
        System.out.println("5. Limpiando archivos temporales...");
        Files.deleteIfExists(Paths.get("DB_EnterpriseHSQLDB.class"));
        Files.deleteIfExists(Paths.get("MANIFEST.MF"));
        
        System.out.println("\n JAR CREADO EXITOSAMENTE");
        System.out.println("\n ARCHIVOS GENERADOS:");
        System.out.println("   - EmpresaHSQLDB.jar (tu aplicación)");
        System.out.println("   - config.ini (configuración)");
        System.out.println("\n PARA EJECUTAR:");
        System.out.println("   java -jar EmpresaHSQLDB.jar");
        System.out.println("\n PARA PORTAR A OTRA MÁQUINA, COPIA:");
        System.out.println("   1. EmpresaHSQLDB.jar");
        System.out.println("   2. config.ini");
        System.out.println("   3. hsqldb-2.7.2.jar");
    }
}

Step-by-Step Breakdown

1
Step 1: Compile the Source Code
2
The utility uses ProcessBuilder to invoke the Java compiler:
3
ProcessBuilder compilar = new ProcessBuilder(
    "javac", "-cp", "hsqldb-2.7.2.jar", "DB_EnterpriseHSQLDB.java"
);
Process procesoCompilar = compilar.start();
procesoCompilar.waitFor();
4
  • -cp hsqldb-2.7.2.jar: Adds the HSQLDB driver to the classpath
  • waitFor(): Waits for compilation to complete before proceeding
5
Step 2: Generate the Manifest File
6
The manifest file defines the JAR’s entry point and dependencies:
7
String manifest = 
    "Manifest-Version: 1.0\n" +
    "Main-Class: DB_EnterpriseHSQLDB\n" +
    "Class-Path: hsqldb-2.7.2.jar\n";

Files.write(Paths.get("MANIFEST.MF"), manifest.getBytes());
8
Manifest Components:
9
  • Manifest-Version: Specifies the manifest specification version (always 1.0)
  • Main-Class: Defines the entry point class with the main() method
  • Class-Path: Lists external JAR dependencies (HSQLDB driver)
  • 10
    The Class-Path entry allows the application to find the HSQLDB driver when executed with java -jar
    11
    Step 3: Create Configuration File
    12
    Generates the config.ini file if it doesn’t exist:
    13
    String config = 
        "#Configuración HSQLDB\n" +
        "database.path=./db_empresa\n";
    
    if (!Files.exists(Paths.get("config.ini"))) {
        Files.write(Paths.get("config.ini"), config.getBytes());
    }
    
    14
    This preserves existing configuration while ensuring a default exists.
    15
    Step 4: Build the JAR Archive
    16
    Uses the jar command to package everything:
    17
    ProcessBuilder crearJar = new ProcessBuilder(
        "jar", "cfm", "EmpresaHSQLDB.jar", "MANIFEST.MF", 
        "DB_EnterpriseHSQLDB.class", "config.ini"
    );
    Process procesoJar = crearJar.start();
    procesoJar.waitFor();
    
    18
    JAR Command Flags:
    19
  • c: Create new archive
  • f: Specify archive file name
  • m: Include manifest file
  • 20
    Step 5: Clean Up Temporary Files
    21
    Removes intermediate files that are no longer needed:
    22
    Files.deleteIfExists(Paths.get("DB_EnterpriseHSQLDB.class"));
    Files.deleteIfExists(Paths.get("MANIFEST.MF"));
    
    23
    This keeps the directory clean while preserving the JAR and configuration files.

    Running the JAR Builder

    1
    Prepare Your Environment
    2
    Ensure you have the following files in your directory:
    3
    ls -l
    
    4
    Expected files:
    5
  • CrearJar.java
  • DB_EnterpriseHSQLDB.java
  • hsqldb-2.7.2.jar
  • 6
    Compile the JAR Builder
    7
    javac CrearJar.java
    
    8
    Execute the Builder
    9
    java CrearJar
    
    10
    Expected Output
    11
    === GENERADOR DE JAR PORTABLE ===
    1. Compilando DB_EnterpriseHSQLDB...
    2. Creando MANIFEST.MF...
    3. Creando config.ini...
    4. Generando JAR...
    5. Limpiando archivos temporales...
    
     JAR CREADO EXITOSAMENTE
    
     ARCHIVOS GENERADOS:
       - EmpresaHSQLDB.jar (tu aplicación)
       - config.ini (configuración)
    
     PARA EJECUTAR:
       java -jar EmpresaHSQLDB.jar
    
     PARA PORTAR A OTRA MÁQUINA, COPIA:
       1. EmpresaHSQLDB.jar
       2. config.ini
       3. hsqldb-2.7.2.jar
    

    Deployment Package Contents

    After running the builder, you’ll have the following files:
    project/
    ├── EmpresaHSQLDB.jar      # Your executable application
    ├── config.ini              # Database configuration
    ├── hsqldb-2.7.2.jar       # HSQLDB driver dependency
    ├── CrearJar.java          # JAR builder source (optional)
    └── DB_EnterpriseHSQLDB.java  # Application source (optional)
    

    Executing the Packaged Application

    Simple Execution

    java -jar EmpresaHSQLDB.jar
    
    This works because:
    1. The manifest specifies DB_EnterpriseHSQLDB as the main class
    2. The Class-Path in the manifest points to hsqldb-2.7.2.jar
    3. The JAR contains the compiled class and configuration file

    Verifying JAR Contents

    To inspect what’s inside the JAR:
    jar tf EmpresaHSQLDB.jar
    
    Expected output:
    META-INF/
    META-INF/MANIFEST.MF
    DB_EnterpriseHSQLDB.class
    config.ini
    

    Viewing the Manifest

    jar xf EmpresaHSQLDB.jar META-INF/MANIFEST.MF
    cat META-INF/MANIFEST.MF
    
    Output:
    Manifest-Version: 1.0
    Main-Class: DB_EnterpriseHSQLDB
    Class-Path: hsqldb-2.7.2.jar
    

    Deploying to Another System

    1
    Package the Distribution
    2
    Create a directory with all required files:
    3
    mkdir empresa-app-dist
    cp EmpresaHSQLDB.jar empresa-app-dist/
    cp config.ini empresa-app-dist/
    cp hsqldb-2.7.2.jar empresa-app-dist/
    
    4
    Create a Portable Archive
    5
    tar -czf empresa-app.tar.gz empresa-app-dist/
    
    6
    Or on Windows:
    7
    Compress-Archive -Path empresa-app-dist -DestinationPath empresa-app.zip
    
    8
    Deploy to Target System
    9
  • Transfer the archive to the target system
  • Extract the archive:
    tar -xzf empresa-app.tar.gz
    cd empresa-app-dist
    
  • Run the application:
    java -jar EmpresaHSQLDB.jar
    
  • Customizing the Configuration

    Before distributing, you can customize config.ini for different environments:
    #Configuración HSQLDB
    database.path=./db_empresa_dev
    

    Advanced JAR Options

    Including Multiple Configuration Files

    Modify CrearJar.java to include additional files:
    ProcessBuilder crearJar = new ProcessBuilder(
        "jar", "cfm", "EmpresaHSQLDB.jar", "MANIFEST.MF", 
        "DB_EnterpriseHSQLDB.class", 
        "config.ini",
        "config.prod.ini",
        "config.dev.ini"
    );
    

    Adding Version Information

    Enhance the manifest with version details:
    String manifest = 
        "Manifest-Version: 1.0\n" +
        "Main-Class: DB_EnterpriseHSQLDB\n" +
        "Class-Path: hsqldb-2.7.2.jar\n" +
        "Implementation-Title: Enterprise Database Manager\n" +
        "Implementation-Version: 1.0.0\n" +
        "Implementation-Vendor: Your Company\n";
    

    Creating a Launcher Script

    Create a shell script for easier execution:
    #!/bin/bash
    # run.sh
    java -Xmx512m -jar EmpresaHSQLDB.jar
    
    Make it executable:
    chmod +x run.sh
    ./run.sh
    

    Troubleshooting

    Class Not Found Error

    Error: Could not find or load main class DB_EnterpriseHSQLDB
    
    Solution: Verify the manifest’s Main-Class entry matches your class name exactly.

    HSQLDB Driver Not Found

    java.lang.ClassNotFoundException: org.hsqldb.jdbc.JDBCDriver
    
    Solution: Ensure hsqldb-2.7.2.jar is in the same directory as your application JAR.

    Configuration File Not Found

    java.io.FileNotFoundException: config.ini
    
    Solution: The application looks for config.ini in the current working directory. Ensure you run the JAR from the correct location.

    Best Practices

    Distribution Checklist:
    1. Test the JAR on a clean system without development tools
    2. Include a README with system requirements and instructions
    3. Provide sample configuration files for different environments
    4. Version your JAR files (e.g., EmpresaHSQLDB-v1.0.jar)
    5. Include the HSQLDB license if distributing the driver
    6. Document any Java version requirements

    Next Steps

    Build docs developers (and LLMs) love