简体   繁体   中英

Stand alone jpackage generated java app generates ClassNotFoundException for com.mysql.cj.jdbc.Driver

I built a java application with jpackage. The application works within eclipseT. The outcome of jpackage can be installed on windows and it starts. The application use a javafx ui. The app is based on Java 17 und use modules. So far, so good. The app shoud write to a MySQL DB and here starts the problem. The mysql-connector-java-8.0.27.jar does not support modules, hence can not be declared like the other jars under --module-path. I used --java-options -Dclasspath=C:\Users\home\mysql-connector-java-8.0.27.jar to overcome. Within the app.cfg file the entry: java-options=-Dclasspath=C:\Users\home\mysql-connector-java-8.0.27.jar is generated. Within the ConnectDB class I use conn = DriverManager.getConnection(DB_JDBC_CONNECT, connectionProps); This works within eclipse, but not within the jpackage generated java app. So I added Class.forName("com.mysql.cj.jdbc.Driver"), but this throws the ClassNotFoundException. Any Idea how to convince the java app to look into mysql-connector-java-8.0.27.jar for the com.mysql.cj.jdbc?Driver?

Jpackage works now as expected. Here the points as lessons learned:

The command which finally works looks like:

jpackage
--name MyApp
--type msi 
--win-console 
--input ..\lib 
--dest \temp\instMyApp 
--module-path %JAVA_HOME%\jmods;.\myApp.ui.jar;.\myApp.db.jar  
--add-modules myApp.ui,myApp.db,java.logging 
--jlink-options --bind-services 
--module myApp.ui/myApp.ui.EntryForm

Maybe the --jlink-options line can be omitted, or extended. Not yet tested.

The big difference is the line

 --input ..\lib

For --input, only the directory containing the non modular jars shall be given. Before I declared as

--input ..\lib\mysql-connector-java-8.0.27.jar

This throws an error on building the msi telling the object already exist.

Before I tried also to declare the classpath by:

java-options=-Dclasspath=C:\Users\home\mysql-connector-java-8.0.27.jar

The declaration can be found in the MayApp.cfg file, but has no effect!

Using the jpackage command as shown, all on one line, results with the entry within the MyApp.cfg file as follows:

[Application]
app.mainmodule=myApp.greek.ui/myApp.ui.EntryForm
app.classpath=$APPDIR\mysql-connector-java-8.0.27.jar

[JavaOptions]
java-options=-Djpackage.app-version=1.0

Additional Notes: --win-console opens a console on starting the MyApp.exe. Pay attention to the

--add-modules myApp.ui,myApp.db,java.logging

line mentioning the java.logging.

With

--java-options -Djava.util.logging.config.file=C:\temp\myApp.log

logging to a file should be possible. Didn't work until now.

Prior to execute the jpackage command I built the jar of the two moduls (myApp.db and myApp.ui) in the same directory where the command will be executed. Eventually the building of jars can be omitted on giving the path to the directories of the modules only. TBT!

The..\lib directory shall only contain non modular jar's as the shown mysql-connector. Other jars, like H2-db-engine may be placed within the same directory for a more light weight version as MySQL.

I tried also to declared the classpath within the MANIFEST.MF. Can apparently be omitted. Is not considered.

Have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM