我已经从 Maven 存储库安装了 MariaDB-JDBC-Java 驱动程序 V 3.3.3。当我发出
mvn clean javafx:jlink
时,生成的 Linux 应用程序未启动并出现错误,未找到驱动程序:
java.lang.ClassNotFoundException: org.mariadb.jdbc.Driver
命令
mvn clean javafx:run
在 Linux 和 Windows 中运行良好。
请告诉我为什么当我将 mariadb java 连接器的依赖项写入 pom.xml 时,mvn 目标“javafx:jlink”就被破坏了?
我尝试用
pom.xml
中的 mysql java 连接器替换 mariadb java 客户端,它具有完全相同的行为。
我希望打包的 java 应用程序通过驱动程序连接到 mariadb 容器,因为它在 cmd 行和 IntelliJ IDEA 上成功完成 - Windows 和 Arch Linux。
-----------------module-info.java
module org.leder.gldisplaymanager {
requires javafx.controls;
requires javafx.fxml;
requires java.sql;
opens org.leder.gldisplaymanager to javafx.fxml;
exports org.leder.gldisplaymanager;
exports org.leder.gldisplaymanager.gui;
opens org.leder.gldisplaymanager.gui to javafx.fxml;
uses java.sql.Driver;
}
更新:我编译了一个 IntelliJ 项目导入和 Linux 应用程序执行的最小示例,如下 -
预期结果:
java.sql.SQLNonTransientConnectionException: Socket fail to connect to host:address=(host=localhost)(port=3306)(type=primary). Verbindungsaufbau abgelehnt
实际结果:
java.sql.SQLException: No suitable driver found for jdbc:mariadb://localhost/display_manager
更新2:
pom.xml依赖如下:
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.3.3</version>
</dependency>
这是一个部分手动的、不完美的解决方案,但仍然可以执行。
(1) 打开您的 https://drive.google.com/drive/folders/162vRPYNnSbE0xddLoNGQ2oPTWUtSmSlt?usp=sharing(intelliJ_project 和 Linux_app)
(2) 下载 mariadb.zip
(3) 解压 mariadb.zip
(4)
cd mariadb
(5)
mvn package
生成输出 -> target/mariadb-1.0-SNAPSHOT.jar
(6)
mvn dependency:copy-dependencies -DoutputDirectory=target/libs
generate -> target/libs 包含该目录下的所有jar文件
(7)
mvn javafx:jlink
生成->目标/应用程序目录
(8)执行命令
cp target/mariadb-1.0-SNAPSHOT.jar target/app/lib/
cp target/libs/*.jar target/app/lib/
cp target/app/bin/app target/app/bin/app.org
(9)编辑target/app/bin/app文件
#!/usr/bin/env sh
##############################################################################
##
## hellofx start up script for UN*X
##
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/.." >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
APP_NAME="hellofx"
APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and HELLOFX_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn () {
echo "$*"
}
die () {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac
CLASSPATH="$APP_HOME/lib/*"
JAVA_HOME="$APP_HOME"
JAVACMD="$JAVA_HOME/bin/java"
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVA_HOME="$APP_HOME"
JAVACMD="$JAVA_HOME/bin/java"
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Escape application args
save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $CDS_JVM_OPTS $JAVA_OPTS $HELLOFX_OPTS -classpath "\"$CLASSPATH\"" org.leder.mariadb.HelloApplication "$APP_ARGS"
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi
exec "$JAVACMD" "$@"
(10)试运行
./target/app/bin/app
将得到预期结果:java.sql.SQLNonTransientConnectionException:套接字无法连接到主机:地址=(主机= localhost)(端口= 3306)(类型=主要)。
(1) 应用程序内容是通过Gradle插件修改的
The Badass Runtime Plugin
。我参考了https://github.com/beryx-gist/badass-runtime-example-javafx(beryx-gist /
badass-运行时-示例-javafx
公开)
(2) 这个答案还不够好。 如果您已经知道如何执行手动步骤,您可以将上述内容重写为自动化的 Maven 配置。