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
Step-by-Step Breakdown
ProcessBuilder compilar = new ProcessBuilder(
"javac", "-cp", "hsqldb-2.7.2.jar", "DB_EnterpriseHSQLDB.java"
);
Process procesoCompilar = compilar.start();
procesoCompilar.waitFor();
-cp hsqldb-2.7.2.jar: Adds the HSQLDB driver to the classpathwaitFor(): Waits for compilation to complete before proceeding
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());
main() methodThe
Class-Path entry allows the application to find the HSQLDB driver when executed with java -jarString 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());
}
ProcessBuilder crearJar = new ProcessBuilder(
"jar", "cfm", "EmpresaHSQLDB.jar", "MANIFEST.MF",
"DB_EnterpriseHSQLDB.class", "config.ini"
);
Process procesoJar = crearJar.start();
procesoJar.waitFor();
Files.deleteIfExists(Paths.get("DB_EnterpriseHSQLDB.class"));
Files.deleteIfExists(Paths.get("MANIFEST.MF"));
Running the JAR Builder
=== 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:Executing the Packaged Application
Simple Execution
This works because:
- The manifest specifies
DB_EnterpriseHSQLDBas the main class - The
Class-Pathin the manifest points tohsqldb-2.7.2.jar - The JAR contains the compiled class and configuration file
Verifying JAR Contents
To inspect what’s inside the JAR:Viewing the Manifest
Deploying to Another System
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/
Customizing the Configuration
Before distributing, you can customizeconfig.ini for different environments:
Advanced JAR Options
Including Multiple Configuration Files
ModifyCrearJar.java to include additional files:
Adding Version Information
Enhance the manifest with version details:Creating a Launcher Script
Create a shell script for easier execution:Troubleshooting
Class Not Found Error
Main-Class entry matches your class name exactly.
HSQLDB Driver Not Found
hsqldb-2.7.2.jar is in the same directory as your application JAR.
Configuration File Not Found
config.ini in the current working directory. Ensure you run the JAR from the correct location.
Best Practices
Next Steps
- Review HSQLDB Setup for database details
- Explore H2 Database alternative
- Learn about JDBC best practices