【问题标题】:javafx jar will not run, no main class foundjavafx jar 不会运行,找不到主类
【发布时间】:2021-02-21 19:37:33
【问题描述】:

我知道这个问题已经被问过很多次了,但没有一个解决方案对我有用。我正在使用 gradle 为 javafx 应用程序构建一个可运行的 jar。我正在使用 Java 8 jdk 和 jre。当我用 7zip 打开 jar 时,我看到它包含了我的类路径。

这是我的文件:

build.gradle

plugins {
    id 'java'
    id 'edu.sc.seis.launch4j' version '2.4.9'
    id 'application'
}

application {
    mainClass = 'sample.Main'
}

jar {
    manifest {
        attributes (
                'Main-Class': 'sample.Main',
                "Class-Path": configurations.compile.collect { it.getName() }.join(' '))
    }
}

launch4j {
    outfile = 'MortgageCalculator.exe'
    mainClassName = 'sample.Main'
}

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("Maximum House Bid Calculator");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }


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

项目结构

来自 build/libs 的 jar 内容

尝试运行 jar 时的输出

从命令行使用gradle run的输出

D:\dev\repos\java\MortageBidCalculator>gradle run

> Task :run FAILED
Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        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(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
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$1(LauncherImpl.java:182)
        at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
        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$8(LauncherImpl.java:863)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(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$4(WinApplication.java:186)
        ... 1 more
Exception running application sample.Main

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_251\bin\java.exe'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.8/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 4s
2 actionable tasks: 1 executed, 1 up-to-date

我认为 gradle run 错误只是因为它没有找到 fxml 文件,所以我很确定这与 no main class found 错误无关,但我不完全确定。

【问题讨论】:

    标签: java gradle javafx jar


    【解决方案1】:

    要从命令行运行 jar 文件,您需要 -jar 选项:

    java -jar MortgageBidCalculator.jar
    

    导致空指针异常的另一个问题是FXML文件不在jar文件中。

    src/main下创建resources文件夹,sample子文件夹,并将sample.fxml移动到src/main/resources/sample

    【讨论】:

    • 但是如何通过双击使其可执行?此外,我将 fxml 的位置更改为 gradle 识别的资源文件夹,所以现在它在 jar 中,但我仍然收到空指针错误。
    • @abg 你把它放在sample 子文件夹中,就像我建议的那样,对吗?那么 FXML 文件与Main 类位于同一文件夹中吗?如果您将 Java 8 作为机器上的默认 JRE,它应该可以通过双击来执行。但是如果只是抛出异常,显然什么都不会发生。
    • 由于某种原因,它最终出现在 jar 的顶级目录中。我不知道如何改变它。
    • @abg 再次,您将它放在sampleresources 子文件夹中,对吗?使用 jar 文件的新布局和屏幕截图更新您的问题(或者仅使用命令行中的jar -tf MortgageBidCalculator.jar 显示内容。
    • 不,它只是在资源中。将其放入样本中有效。谢谢!
    猜你喜欢
    • 2013-06-30
    • 2020-07-28
    • 1970-01-01
    • 2019-02-21
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2018-08-23
    相关资源
    最近更新 更多