如何重新运行 JavaFX App
How to re-run JavaFX App
我的主线程启动 javafx.application.Application
,它会在其作业完成后自行终止。当我尝试再次启动同一个 JavaFX 应用程序时,我总是收到 IllegalStateException: Application launch must not be called more than once.
简单示范示例:
public class MainApp {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
FXApp.setCounter(i);
System.out.println("launching i="+i);
FXApp.launch(FXApp.class, args);
}
}
}
public class FXApp extends Application {
private static int counter;
public static void setCounter(int counter) {
FXApp.counter = counter;
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
Platform.exit();
}
}
@Override
public void stop() throws Exception {
super.stop();
System.out.println("stopping counter="+counter);
}
}
控制台输出
launching i=0
stopping counter=0
launching i=1
Exception in thread "main" java.lang.IllegalStateException: Application launch must not be called more than once
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:162)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:143)
at javafx.application.Application.launch(Application.java:191)
at ...MainApp.main(MainApp.java:9)
我该如何解决这个问题?
Application
class 代表整个 运行 应用程序。名为 launch()
的静态方法用于启动应用程序。由于根据定义,您可以在应用程序的生命周期内只启动任何给定的应用程序一次,因此在应用程序的生命周期内多次调用 launch
是非法的,这样做会抛出异常,就像clearly and explicitly detailed in the documentation.
调用 launch()
创建代表 运行 应用程序的 Application
subclass 的实例,启动 JavaFX 工具包,并调用 start()
应用程序实例。因此,start()
方法实际上是应用程序的入口点。请注意,start()
是在 FX 应用程序线程上调用的(因此您不能在此处使用 long-运行 或阻塞代码)。同样,所有细节都在 documentation.
中仔细说明
您还没有说明您实际尝试做什么,并且由于您使用的方法和 classes 的方式与它们的预期用途完全相反,所以几乎不可能猜猜这个问题可能意味着什么。然而,等效的功能可以通过类似
的方式实现
public class FXApp extends Application {
@Override
public void start(Stage primaryStage) {
Thread pausingAndIteratingThread = new Thread(() -> {
for (int i = 0 ; i < 10 ; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException exc) {
exc.printStackTrace();
} finally {
final int counter = i ;
Platform.runLater(() -> endIteration(counter));
}
}
};
pausingAndIterartingThread.start();
}
private void endIteration(int counter) {
System.out.println("stopping: counter="+counter);
}
}
然后是
public class MainApp {
public static void main(String[] args) {
Application.launch(FXApp.class, args);
}
}
我的主线程启动 javafx.application.Application
,它会在其作业完成后自行终止。当我尝试再次启动同一个 JavaFX 应用程序时,我总是收到 IllegalStateException: Application launch must not be called more than once.
简单示范示例:
public class MainApp {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
FXApp.setCounter(i);
System.out.println("launching i="+i);
FXApp.launch(FXApp.class, args);
}
}
}
public class FXApp extends Application {
private static int counter;
public static void setCounter(int counter) {
FXApp.counter = counter;
}
@Override
public void start(Stage primaryStage) throws Exception {
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
} finally {
Platform.exit();
}
}
@Override
public void stop() throws Exception {
super.stop();
System.out.println("stopping counter="+counter);
}
}
控制台输出
launching i=0
stopping counter=0
launching i=1
Exception in thread "main" java.lang.IllegalStateException: Application launch must not be called more than once
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:162)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:143)
at javafx.application.Application.launch(Application.java:191)
at ...MainApp.main(MainApp.java:9)
我该如何解决这个问题?
Application
class 代表整个 运行 应用程序。名为 launch()
的静态方法用于启动应用程序。由于根据定义,您可以在应用程序的生命周期内只启动任何给定的应用程序一次,因此在应用程序的生命周期内多次调用 launch
是非法的,这样做会抛出异常,就像clearly and explicitly detailed in the documentation.
调用 launch()
创建代表 运行 应用程序的 Application
subclass 的实例,启动 JavaFX 工具包,并调用 start()
应用程序实例。因此,start()
方法实际上是应用程序的入口点。请注意,start()
是在 FX 应用程序线程上调用的(因此您不能在此处使用 long-运行 或阻塞代码)。同样,所有细节都在 documentation.
您还没有说明您实际尝试做什么,并且由于您使用的方法和 classes 的方式与它们的预期用途完全相反,所以几乎不可能猜猜这个问题可能意味着什么。然而,等效的功能可以通过类似
的方式实现public class FXApp extends Application {
@Override
public void start(Stage primaryStage) {
Thread pausingAndIteratingThread = new Thread(() -> {
for (int i = 0 ; i < 10 ; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException exc) {
exc.printStackTrace();
} finally {
final int counter = i ;
Platform.runLater(() -> endIteration(counter));
}
}
};
pausingAndIterartingThread.start();
}
private void endIteration(int counter) {
System.out.println("stopping: counter="+counter);
}
}
然后是
public class MainApp {
public static void main(String[] args) {
Application.launch(FXApp.class, args);
}
}