java - 运行 JavaFX Gradle 应用程序时的 NoClassDefFoundError StringUtils 或 Gson
问题描述
我正在构建一个 JavaFX 应用程序,但我似乎无法添加和使用 Gson 或 Apache Commons Lang 等库。在我的和 Intellij中引用时,这些库似乎添加得很好,build.gradle
也显示它们在我的项目中。Gradle 似乎也可以运行clean
并且build
没有问题。当我实际运行应用程序并单击触发问题开始的事件子例程的按钮时,就会出现问题。无论我尝试引用哪个库,我也会遇到这样的错误:
Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson
这是我的代码
构建.gradle
apply plugin: 'java'
apply plugin: 'javafx-gradle-plugin'
//apply plugin: 'excelsiorJet'
group 'eagleview'
version '1.0-SNAPSHOT'
if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
throw new GradleException("Must Build with Java 1.8")
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.4'
// https://mvnrepository.com/artifact/commons-lang/commons-lang
compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
buildscript {
//ext.jetPluginVersion = '1.1.0'
dependencies {
classpath group: 'de.dynamicfiles.projects.gradle.plugins', name: 'javafx-gradle-plugin', version: '8.8.2'
//classpath "com.excelsiorjet:excelsior-jet-gradle-plugin:$jetPluginVersion"
}
repositories {
mavenLocal()
mavenCentral()
}
}
jar {
manifest {
attributes(
'Main-Class': 'eagleview.App',
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
)
}
}
jfx {
// Minimal requirement for jfxJar-task
mainClass = 'eagleview.App'
// Minimal requirement for jfxNative-task
vendor = 'eagleview'
}
//excelsiorJet {
// mainClass = 'eagleview.App'
// hideConsole = true
//}
配置控制器.java
import com.google.gson.Gson;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextInputDialog;
import javafx.stage.Stage;
import org.apache.commons.lang.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class ConfigurationController {
private static Stage stage;
public void render(Stage primary) throws Exception {
stage = primary;
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("views/vwConfiguration.fxml"));
stage.setTitle ("Config - Eagle View");
stage.setScene(new Scene(root, 800, 600));
stage.show();
}
@FXML
private void handleButtonAddVideoClicked(ActionEvent event) throws Exception {
Gson gson = new Gson();
System.out.println(gson.toJson(new SettingsModel()));
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("What video would you like to add?");
dialog.setContentText("Please provide a YouTube URL to add to your video list.");
dialog.initOwner(stage);
dialog.showAndWait().ifPresent(x -> {
if(StringUtils.isNotBlank(x)) {
System.out.println(x);
importVideoQueue.add(x);
}
});
event.consume();
}
}
运行时产生以下错误handleButtonAddVideoClicked
:
mymachine$ java -jar build/libs/eagleview-1.0-SNAPSHOT.jar /c
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8865)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3876)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1300(Scene.java:3604)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2613)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
... 45 more
Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson
at eagleview.controllers.ConfigurationController.handleButtonAddVideoClicked(ConfigurationController.java:50)
... 56 more
Caused by: java.lang.ClassNotFoundException: com.google.gson.Gson
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:190)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:499)
... 57 more
如果我删除 Gson 代码,我将收到一个关于 StringUtils 的错误,除了引用该特定库包路径外,它看起来与上面完全相同。
这是怎么回事?我的 Gradle 脚本配置不正确吗?
谢谢!
解决方案
您在类路径中错过了 apache commons-lang。添加
// https://mvnrepository.com/artifact/commons-lang/commons-lang
compile group: 'commons-lang', name: 'commons-lang', version: '1.0.1'
到你的 gradle 文件。
2.6版本有包名
org.apache.commons.lang2
推荐阅读
- google-analytics - 确保我的 Google Analytics 视图设置正确
- verilog - 为什么内联 Verilog 算术移位将其变成逻辑移位?
- c# - 跳过一项时,ASP.NET MVC 5 未获取所有绑定复选框
- python - 将 NLP WordNetLemmatizer 应用于整个句子显示错误且位置未知
- javascript - 如何在Vue.js(Laravel6)中的数据中返回数据
- apache - Apache HTTP2 h2c 模式无法正常工作
- sql - 相当基本的查询(可能需要子查询和/或外连接)?
- amazon-web-services - 以下 AWS CLI 命令的 boto3 等效项是什么?
- python - 如何使用其他列中的值来填充 Pandas 中一列的 nan
- slurm - Slurm sacct 不同的两条线