首页 > 解决方案 > 如何修复 JavaFX 中的“应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常”错误?

问题描述

我刚开始使用 JavaFX,我正在尝试为学校项目构建一个 ARP 欺骗工具。但是当我运行代码时,我遇到了这个问题。我尝试阅读 Stack Overflow 上的许多答案。我什至尝试从一开始就重新制作这个项目。但我不断收到同样的错误。

这是代码。

控制器.java

package controller;

import org.jnetpcap.Pcap;
import org.jnetpcap.PcapIf;

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

public class Main extends Application {

public static Pcap pcap = null;
public static PcapIf device = null;

private Stage primaryStage;
private AnchorPane layout;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;
    this.primaryStage.setTitle("JavaFX ARP Spoofing Tool");
    setLayout();
}

public void setLayout() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("../view/View.fxml"));
        layout = (AnchorPane) loader.load();
        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
 }

 public Stage getPrimaryStage() {
    return primaryStage;
 }

 public static void main(String[] args) {
     launch(args);
 }
}

查看.fxml

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

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.collections.*?>

<AnchorPane prefHeight="480.0" prefWidth="490.0" 
    fx:controller="controller.Controller"
    xmlns="http://javafx.com/javafx/8.0.111" 
    xmlns:fx="http://javafx.com/fxml/1">
    <children>
    <ListView fx:id="networkListView" layoutX="15.0" layoutY="14.0" 
      prefHeight="78.0" prefWidth="462.0">
        <items>
            <FXCollection fx:factory="observableArrayList" />
        </items>
    </ListView>
    <Button fx:id="pickButton" onAction="#networkPickAction" 
      layoutX="395.0" layoutY="103.0" 
      prefHeight="29.0" prefWidth="82.0" text="PICK" />
    <TextArea fx:id="textArea" editable="false" layoutX="15.0" 
      layoutY="144.0" 
      prefHeight="325.0" prefWidth="462.0" />
    </children>
</AnchorPane>

主.java

    package controller;

    import java.net.URL;
    import java.util.ArrayList;
    import java.util.ResourceBundle;

    import org.jnetpcap.Pcap;
    import org.jnetpcap.PcapIf;

    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.fxml.FXML;
    import javafx.fxml.Initializable;
    import javafx.scene.control.Button;
    import javafx.scene.control.ListView;
    import javafx.scene.control.TextArea;

    public class Controller implements Initializable {

    @FXML
    private ListView<String> networkListView;

    @FXML
    private TextArea textArea;

    @FXML
    private Button pickButton;

    ObservableList<String> networkList = FXCollections.observableArrayList();

    private ArrayList<PcapIf> allDevs = null;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        allDevs = new ArrayList<PcapIf>();
        StringBuilder errbuf = new StringBuilder();
        int r = Pcap.findAllDevs(allDevs, errbuf);
        if (r == Pcap.NOT_OK || allDevs.isEmpty()) {
            textArea.appendText("Failed to find network device.\n" + errbuf.toString() + "\n");
            return;
        }
        textArea.appendText("A network device detected.\nChoose the device.\n");
        for (PcapIf device : allDevs) {
            networkList.add(device.getName() + " " +
                    ((device.getDescription() != null) ? device.getDescription(): "No Description"));
        }
        networkListView.setItems(networkList);
    }

    public void networkPickAction() {
        if(networkListView.getSelectionModel().getSelectedIndex() < 0) {
            return;
        }
        Main.device = allDevs.get(networkListView.getSelectionModel().getSelectedIndex());
        networkListView.setDisable(true);
        pickButton.setDisable(true);

        int snaplen = 64 * 1024;
        int flags = Pcap.MODE_PROMISCUOUS;
        int timeout = 1;

        StringBuilder errbuf = new StringBuilder();
        Main.pcap = Pcap.openLive(Main.device.getName(), snaplen, flags, timeout, errbuf);

        if(Main.pcap == null) {
            textArea.appendText("Failed to open the network device.\n" + errbuf.toString() + "\n");
            return;
        }
        textArea.appendText("Choose the device: " + Main.device.getName() + "\n");
        textArea.appendText("Activated the device.\n");
    }
}

当我单击带有 main→run as java application 的类时,会出现以下问题:

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$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.UnsatisfiedLinkError: com.slytechs.library.NativeLibrary.dlopen(Ljava/lang/String;)J
    at com.slytechs.library.NativeLibrary.dlopen(Native Method)
    at com.slytechs.library.NativeLibrary.<init>(Unknown Source)
    at com.slytechs.library.JNILibrary.<init>(Unknown Source)
    at com.slytechs.library.JNILibrary.loadLibrary(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at com.slytechs.library.JNILibrary.register(Unknown Source)
    at org.jnetpcap.Pcap.<clinit>(Unknown Source)
    at controller.Controller.initialize(Controller.java:37)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at controller.Main.setLayout(Main.java:31)
    at controller.Main.start(Main.java:24)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(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$152(WinApplication.java:177)
    ... 1 more
Exception running application controller.Main

标签: javafx

解决方案


推荐阅读