我正在构建一个JavaFX应用程序,但我似乎无法添加和使用像Gson或Apache Commons Lang这样的库。在build.gradle
中引用库时,库似乎添加正常,Intellij也在我的项目中显示它们.Gradle似乎也运行clean
和build
没有任何问题。当我实际运行应用程序并单击触发问题开始的事件子例程的按钮时,会出现问题。我也会遇到这样的错误,无论我试图引用哪个库:
Caused by: java.lang.NoClassDefFoundError: com/google/gson/Gson
的build.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
//}
ConfigurationController.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脚本配置不正确吗?
谢谢!
答案 0 :(得分:1)
你错过了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
答案 1 :(得分:0)
我找到了问题的解决方案。我试图建立一个JAR,但我目前的jar
块并没有削减它。我需要建立一个"脂肪"动态拉入库的jar。移除了jar
块并添加了下面的fatJar
块。
task fatJar(type: Jar) {
manifest {
attributes 'Main-Class': 'eagleview.App'
}
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
} with jar
}
我使用./gradlw clean fatJar
构建,应用程序运行完美,没有任何错误!