引入JFoenix库后无法运行JavaFX应用程序
解决JFoenix运行时
java.lang.NoClassDefFoundError: com/sun/javafx/css/StyleConverterImpl异常 问题分析
我帮不少开发者排查过这个问题,核心原因其实是JFoenix版本和你使用的JavaFX版本不兼容,再加上打包环节没处理好依赖导致的:
- JavaFX 11及以后的版本,直接移除了
com.sun.javafx下的大量内部API(包括你报错里的StyleConverterImpl),而旧版JFoenix刚好硬依赖这些已经被废弃的内部接口; - 虽然项目构建成功,但如果打包时没把JFoenix、JavaFX这些依赖完整塞进可执行Jar里,运行时自然会找不到必要的类。
解决方案
1. 匹配JFoenix与JavaFX的版本
先把版本对应关系捋顺,这是解决问题的前提:
- 如果你用JavaFX 11-17:选JFoenix 9.0.10及以上的稳定版本(比如9.0.11就很常用)
- 如果你用JavaFX 20+:建议用最新的JFoenix快照版,或者先确认官方标注兼容的版本
划重点:别拿旧版JFoenix(比如9.0.9及以前)搭配JavaFX 11+,旧版JFoenix对内部API的依赖根本绕不开。
2. 正确打包可执行Jar(包含所有依赖)
必须把JFoenix、JavaFX和其他依赖全部打包到同一个Jar里,避免运行时丢类:
- Maven用户:用
maven-shade-plugin插件,配置时记得指定你的主类,确保所有依赖被合并<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>main.Main</mainClass> <!-- 替换成你的主类全路径 --> </transformer> </transformers> </configuration> </execution> </executions> </plugin> - Gradle用户:用
shadow插件,同样要配置主类plugins { id 'java' id 'application' id 'com.github.johnrengelman.shadow' version '7.1.2' } mainClassName = 'main.Main' // 替换成你的主类全路径
3. 确认JavaFX运行环境配置
如果不想打包成Fat Jar,运行时就得显式指定JavaFX的模块路径:
java --module-path "本地JavaFX SDK的lib目录路径" --add-modules javafx.controls,javafx.fxml -jar 你的应用Jar包名.jar
要确保路径指向的lib目录里有javafx.controls和javafx.fxml模块,这俩是JFoenix必须依赖的。
你的异常详情
Exception in Application start method java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source) at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at java.base/sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) Caused by: java.lang.RuntimeException: Exception in Application start method at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(Unknown Source) at java.base/java.lang.Thread.run(Unknown Source) Caused by: java.lang.NoClassDefFoundError: com/sun/javafx/css/StyleConverterImpl at java.base/java.lang.ClassLoader.defineClass1(Native Method) at java.base/java.lang.ClassLoader.defineClass(Unknown Source) at java.base/java.security.SecureClassLoader.defineClass(Unknown Source) at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(Unknown Source) at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(Unknown Source) at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(Unknown Source) at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) at com.jfoenix.controls.JFXButton$StyleableProperties.<clinit>(JFXButton.java:208) at com.jfoenix.controls.JFXButton.<init>(JFXButton.java:156) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.base/java.lang.reflect.Constructor.newInstance(Unknown Source) at java.base/java.lang.Class.newInstance(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml/javafx.fxml.FXMLLoader.load(Unknown Source) at main.Main.start(Main.java:15) at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(Unknown Source) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(Unknown Source) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(Unknown Source) at java.base/java.security.AccessController.doPrivileged(Native Method) at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(Unknown Source) at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source) ... 1 more Caused by: java.lang.ClassNotFoundException: com.sun.javafx.css.StyleConverterImpl at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
内容的提问来源于stack exchange,提问作者syd
相关产品推荐
相关产品推荐

