3

I'm writing a plugin tool using JavaFX for our Flagship Application (at work). The Flagship App has a Java client that uses Swing, and it allows the user to execute macros written in Java, or install plugins written with Java.

Note, these macros/plugins can run on their own and are not dependent upon the Java client, so there is no need to place JavaFX components into a JFXPanel.

The problem we've been having is one of debugging convenience. We run the macro or plugin that uses JavaFX, and after we kill it (using stop() and Platform.exit() ), we have to completely restart the Flagship App Java client (i.e. the JVM) before we can re-run the JavaFX macro/plugin, thanks to the whole "Application launch must not be called more than once" error. The Flagship App loads relatively quickly, but it still needs a minute or two to fully load and open a new file (can't run a macro or plugin without an open file).

So I've seen a few suggestions to place subsequent calls to Application.launch() into a runLater call, or to use the Executor service, but none of these seems to really get at what I need, since the Platform thread appears to be killed off (JavaFX GUI closes properly, and the thread vanishes from the list of running threads). It's almost as if the JVM has a boolean that gets set to true when you call Application.launch(), so that you can't restart a JavaFX app without killing the JVM.

I need to be able run a JavaFX Application multiple times on the same JVM. So is it possible to do that?

  • 1
    I think this is one of the places where a bit of code example could help. Also: when you read things already that are relevant here, consider having a link for reference (so that people don't tell you things you already know). – GhostCat Oct 29 '18 at 03:58
  • 1
    Not sure what kind of code I can offer that would be novel and instructive. I override the Application stop() method and put Platform.exit() in there. Stepping through the code with the debugger shows me that I am executing the stop() method (and Platform.exit()) as expected. I'll see if I can find the links to other articles I've read. – Shane Gillis Oct 29 '18 at 04:09
  • 4
    Code could be framed around [mcve]. The point is: 15 lines of code leave zero possibility for *interpretation and speculation* by readers. But when you *explain* what your code supposedly does, we can't be 100 sure what it is doing. But still, you are already on a good path, and it is an interesting topic. – GhostCat Oct 29 '18 at 04:24
  • 3
    I don't really think there is a simple workaround for this. JavaFX is designed to have such an application lifecycle - where only one instance of JavaFX application run in a single JVM instance, and that underlying low level libraries are initialized and cleaned up based on that single application. Once your application stops, the clean up codes are going to run and you will probably never get a stable state again, even if you use reflections to bypass the restriction. – Jai Oct 29 '18 at 05:38
  • Thought I had an idea by using different `ModuleLayer`s for each application instance but the second launch ends up throwing an `UnsatisfiedLinkError`. So unless I'm doing something wrong it may not even be possible (no matter what) to launch JavaFX more than once in the same JVM. – Slaw Oct 29 '18 at 14:55

1 Answers1

0

Are you able to define the JavaFX macros using Stage rather than Application? If so, this video coordinates the shutdown of a main Swing app that brings up multiple JavaFX windows.

https://www.youtube.com/watch?v=1cobvRlRHic

Wall0p
  • 248
  • 1
  • 7