为什么主类中的LauncherImpl.launchApplication()(javaFX launcher)正在启动InvocationTargetException而没有调用launch()? [英] Why LauncherImpl.launchApplication() (javaFX launcher) in the main class are launching InvocationTargetException and calling launch() not?
问题描述
我正在使用javaFX(13.0.2)创建一个项目,我需要使用预加载器,但是 LauncherImpl.launchApplication()
失败.
I´m using javaFX (13.0.2) to make a project and I need to use a preloader, but the LauncherImpl.launchApplication()
is not successful.
代码:
package classes;
import com.sun.javafx.application.LauncherImpl;
import javafx.application.Application;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
System.out.println("start");
}
@Override
public void init() throws Exception {
System.out.println("init");
}
public static void main(String[] args) {
LauncherImpl.launchApplication(Main.class, UIloadSystem.class, args); // - Not works
LauncherImpl.launchApplication(Main.class, args); // - Not works
//launch(Main.class, args); - Works
}
}
是的,这是一个非常简单的代码
错误:
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:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
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:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.IllegalAccessError: class classes.Main (in unnamed module @0x1936f0f5) cannot access class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to unnamed module @0x1936f0f5
at classes.Main.main(Main.java:30)
... 11 more
Exception running application classes.Main
我在这部分的顶部研究了类类.Main(在未命名模块@ 0x1936f0f5中)无法访问com.sun.javafx.application.LauncherImpl类(在javafx.graphics模块中),因为模块javafx.graphics没有将com.sun.javafx.application导出到未命名的模块@ 0x1936f0f5
,但是
我在任何地方都找不到此活动.
I researched on top of this part class classes.Main (in unnamed module @0x1936f0f5) cannot access class com.sun.javafx.application.LauncherImpl (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.application to unnamed module @0x1936f0f5
, but
I didn't find this event anywhere.
如果很重要,我使用的是ItelliJ IDEA,VM选项为-module-path $ {PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
If are imporant, I'm using ItelliJ IDEA and the VM options are --module-path ${PATH_TO_FX} --add-modules javafx.controls,javafx.fxml
对不起,如果有一些语法错误:|
Sorry for my english, if are some grammatical errors :|
推荐答案
com.sun.javafx.application.LauncherImpl
是私有API,不适合普通开发人员使用.请注意,它会启动 com.sun
.所有公共JavaFX API都包含在软件包 javafx.*
中.从Java 9开始,不再可以访问私有API,但是仍然存在一些较旧的教程.
com.sun.javafx.application.LauncherImpl
is private API and not intended for normal developers. Note the fact it starts com.sun
. All the public JavaFX API is contained in packages javafx.*
. From Java 9 onwards private API is no longer accessible, however some older tutorials still exist.
要使用预加载器,应在Java 9之后采用以下方法.
To use a preloader the following approach should be taken after Java 9.
public static void main(String[] args){
System.setProperty("javafx.preloader", UIloadSystem.class.getCanonicalName());
launch(args);
}
这篇关于为什么主类中的LauncherImpl.launchApplication()(javaFX launcher)正在启动InvocationTargetException而没有调用launch()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!