使用org.controlsfx的JavaFX-ClassNotFoundException:org.controlsfx.control.ToggleSwitch

问题描述

我在项目中使用controlfx时遇到问题。

我的项目基于Java8。我想将ToggleSwitch与org.controlsfx一起使用。

我在Scene Builder 8.5.0中使用JavaFX,Maven和FXML。

我从本页下载了文件controlsfx-8.40.17.jar:https://search.maven.org/search?q=g:org.controlsfx%20AND%20a:controlsfx%20AND%20v:8 *

#1。我将下载的文件添加到Scene Builder(库> JAR / FXML Manager>从文件系统添加库/ FXML)

#2。在Scene Builder中,我将ToggleSwitch添加到FXML模板

#3。在项目的pom.xml文件中,我添加了依赖项:

    <dependency>
        <groupId>org.controlsfx</groupId>
        <artifactId>controlsfx</artifactId>
        <version>8.40.17</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/lib/controlsfx-8.40.17.jar</systemPath>
    </dependency>

#4。在IntelliJ中运行项目时,一切都很好

#5。该项目由Maven构建。

#6。尝试运行.jar时出现错误

我不做什么?

文件 Main.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root,300,275));
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

文件 Controller.java

package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.scene.control.Button;
import org.controlsfx.control.ToggleSwitch;

public class Controller {

    @FXML
    Button button1;
    @FXML
    ToggleSwitch toggle1;

    public void buttonAction(ActionEvent actionEvent) {
        System.out.println("Click");
    }
}

文件 sample.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import org.controlsfx.control.ToggleSwitch?>

<AnchorPane prefheight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <Button fx:id="button1" layoutX="36.0" layoutY="39.0" mnemonicParsing="false" onAction="#buttonAction" text="Button" />
      <ToggleSwitch fx:id="toggle1" layoutX="105.0" layoutY="156.0" />
   </children>
</AnchorPane>

文件 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sample</groupId>
    <artifactId>TestowyProjekt</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>8.40.17</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/controlsfx-8.40.17.jar</systemPath>
        </dependency>

        <dependency>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>sample.Main</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>lib/controlsfx-8.40.17.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

错误(使用CMD运行):

E:\JAVA\Java\JavaFX\Temp\TestowyProjekt\target>java -jar TestowyProjekt-1.0-SNAPSHOT.jar
Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
        at java.lang.reflect.Method.invoke(UnkNown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
        at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source)
        at java.lang.reflect.Method.invoke(UnkNown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(UnkNown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
        at java.lang.Thread.run(UnkNown Source)
Caused by: javafx.fxml.LoadException:
file:/E:/JAVA/Java/JavaFX/Temp/TestowyProjekt/target/TestowyProjekt-1.0-SNAPSHOT.jar!/sample.fxml

        at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
        at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
        at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
        at javafx.fxml.FXMLLoader.processprocessingInstruction(FXMLLoader.java:2661)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
        at sample.Main.start(Main.java:13)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
        at com.sun.glass.ui.invokelaterdispatcher$Future.run(invokelaterdispatcher.java:95)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
        ... 1 more
Caused by: java.lang.classNotFoundException: org.controlsfx.control.ToggleSwitch
        at java.net.urlclassloader.findClass(UnkNown Source)
        at java.lang.classLoader.loadClass(UnkNown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(UnkNown Source)
        at java.lang.classLoader.loadClass(UnkNown Source)
        at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
        at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
        at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
        ... 20 more
Exception running application sample.Main

文件 src \ main \ java \ meta-inf

Manifest-Version: 1.0
Class-Path: lib/controlsfx-8.40.17.jar lib/javafx-maven-plugin-8.8.3.j
 ar lib/maven-plugin-api-3.0.jar lib/maven-model-3.0.jar lib/maven-art
 ifact-3.0.jar lib/sisu-inject-plexus-1.4.2.jar lib/sisu-inject-bean-1
 .4.2.jar lib/sisu-guice-2.1.7-noaop.jar lib/maven-core-3.0.jar lib/ma
 ven-settings-3.0.jar lib/maven-settings-builder-3.0.jar lib/maven-rep
 ository-Metadata-3.0.jar lib/maven-model-builder-3.0.jar lib/maven-ae
 ther-provider-3.0.jar lib/aether-impl-1.7.jar lib/aether-spi-1.7.jar 
 lib/aether-api-1.7.jar lib/aether-util-1.7.jar lib/plexus-interpolati
 on-1.14.jar lib/plexus-utils-2.0.4.jar lib/plexus-classworlds-2.2.3.j
 ar lib/plexus-component-annotations-1.5.5.jar lib/plexus-sec-dispatch
 er-1.3.jar lib/plexus-cipher-1.4.jar
Build-Jdk-Spec: 1.8
Created-By: Maven Jar Plugin 3.2.0
Main-Class: sample.Main

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)