0

I'm using jpackage to create native Java installer on Windows. And also my app need a JDBC library (example mysql-connector-java-8.0.28.jar) to run the native app to connect the database.

I'm using NetBeans to build the app and after build, I saw the mysql-connector-java-8.0.28.jar are in dist/lib directory.

So that's mean, if I run java -jar myProgram.jar inside dist directory, the app will refer the mysql-connector-java-8.0.28.jar inside the lib library.

The directory in my project be like this:

dist/
  lib/mysql-connector-java-8.0.28.jar
  myProgram.jar

This is the result after using jpackage and install the native app:

myProgram/
   app/myProgram.jar
   runtime/<the jre directory>
   myProgram.exe

And I run the app, I got error that JDBC class not found:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/naming/NamingException
        at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:186)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:681)

I'm using this command to create the installer:

 jpackage \
--type exe \
--runtime-image minimal-jre \
--main-jar myProgram.jar \
--input target \ 
--win-console

So, my question is, how to bundle the lib folder so that .exe program can connect the JDBC class?

The documentation: https://docs.oracle.com/en/java/javase/14/docs/specs/man/jpackage.html

Noobie Cooder
  • 141
  • 1
  • 1
  • 7

1 Answers1

0

I declared all dependencies (including the JDBC driver) to Maven or Gradle, and the tool on it's own would build the application jar and drop it together with all the dependencies in a lib folder. JPackage will use that lib folder to create a package - at runtime consider all these files to be on the classpath.

So if mysql.jar does not end up in that directory for you, maybe you want to run jpackage twice:

  • Run jpackage to create an app-image. That is the directory structure before the actual packaging happens.
  • Add your files into the app-image structure where you need them
  • Run jpackage to finish building the package

With that you can customize a lot that jpackage does not offer out of the box.

Queeg
  • 7,748
  • 1
  • 16
  • 42