javafx jar 不会 运行,找不到主要 class
javafx jar will not run, no main class found
我知道这个问题已经被问过很多次了,但是 none 的解决方案对我有用。我正在使用 gradle 为 javafx 应用构建一个 运行nable jar。我正在使用 Java 8 jdk 和 jre。当我用 7zip 打开罐子时,我看到它包含我的 class 路径。
这是我的文件:
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
时的输出
从命令行使用 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(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(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater(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(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 错误无关,但我我不太确定。
从命令行运行一个jar文件,你需要-jar
选项:
java -jar MortgageBidCalculator.jar
导致空指针异常的另一个问题是 FXML 文件不在 jar 文件中。
在 src/main
下创建一个 resources
文件夹,一个 sample
子文件夹,并将 sample.fxml
移动到 src/main/resources/sample
。
我知道这个问题已经被问过很多次了,但是 none 的解决方案对我有用。我正在使用 gradle 为 javafx 应用构建一个 运行nable jar。我正在使用 Java 8 jdk 和 jre。当我用 7zip 打开罐子时,我看到它包含我的 class 路径。
这是我的文件:
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
时的输出从命令行使用 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(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(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater(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(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 错误无关,但我我不太确定。
从命令行运行一个jar文件,你需要-jar
选项:
java -jar MortgageBidCalculator.jar
导致空指针异常的另一个问题是 FXML 文件不在 jar 文件中。
在 src/main
下创建一个 resources
文件夹,一个 sample
子文件夹,并将 sample.fxml
移动到 src/main/resources/sample
。