In this blog post, I’ll show you how to create an runnable JAR file. This will use the code from video 12.10.

A runnable JAR file allows you to have a single JAR file that includes all Java class files and supporting libraries. In our case, this includes the JAR files for the MySQL JDBC driver (client api code only) and the JAR file for the GUI library JGoodies. Once the JAR file is created, then you’ll be able to double-click the JAR file in the file manager and the application will start.

Follow these steps.

  1. Download the source code from video 12.10 from this link
  2. Unzip the zip file to a directory on your computer. I’ll refer to this directory later on as as ${jdbc-demo-code}
  3. In Eclipse, import the code using:
    1. Select File > Import > General > Existing Projects into Workspace
    2. Select root directory: Browse to the directory where you unzipped the code: ${jdbc-demo-code}
    3. Click Finish
  4. Run your project first to make sure it works
    1. Open the file: EmployeeeSearchApp.java in the package: com.luv2code.jdbc.employeesearch.ui
    2. Right click in the file and select Run As > Java Application
  5. Create a new folder under your current project
    1. Select the root folder of your project: jdbc-swing-gui-check-password
    2. Right click and select New > Folder
    3. For folder name, enter: target
    4. Click Finish.
  6. Copy the props file to the target folder
    1. Select the demo.properties file and copy it to the folder: target
    2. Note: Your JAR file will use this properties file
  7. Export to a Runnable JAR file
    1. Select the root folder of your project: jdbc-swing-gui-check-password
    2. Right click and select Export…
    3. Select Java > Runnable JAR file
    4. Click Next
  8. Set the target options
    1. For Launch configuration, choose the one that says: EmployeeSearchApp …
    2. For Export destination, click the Browse… button
    3. Select the target directory you created in a previous step
    4. For the file name Save As, enter: jdbc-demo-app.jar
    5. For Library handling, select: Extract required libraries into generated JAR
    6. Click Finish
    7. Note: the export will finish with warnings. You can safely ignore them.
  9. If everything worked properly then you will have the following directory structure:
    • jdbc-swing-gui-check-password/target
      • jdbc-demo-app.jar
      • demo.properties
  10. You can run the program using your Windows Explorer or Mac Finder.
    • Move to the directory where your jdbc-demo-app.jar is located
    • Double-click the file and it should run and connect to your database.

* Troubleshooting.

  1. If you have problems then you should run from command-line to see details of the stacktrace.
  2. Open a command-line window and navigate to where the jdbc-demo-app.jar is located
  3. Run from command-line with: java -jar jdbc-demo-app.jar
  4. View the stacktrace and it will tell you what the problem is.