4

I'm trying to launch a JavaFx application from within a JavaFx application, but it looks like Application.launch() can only be called once. Does this mean I have to start a separate JVM... as in exec("java... or is there another way?

More background info. I want my JavaFx app to be able to build and run JavaFx apps. Right now it compiles classes in-memory, loads the classes... it would be really unfortunate to have to resort to writing everything to the filesystem so I can get a jar on the filesystem, so I can use exec to start it up.

As a secondary question... Is there a way to open another JavaFx window and get the stage and pass it to my newly compiled and loaded Application sub-class?

phil-daniels
  • 574
  • 2
  • 10
  • 28
  • I just had a thought... no time to test at the moment. Could I create another class loader, load the java fx classes and mine, then call Application.launch() again? – phil-daniels Nov 18 '12 at 01:48

1 Answers1

17

If you want to execute another JavaFX application in the same JVM you can just create instance of it, manually create Stage and call Application#start()

public void runAnotherApp(Class<? extends Application> anotherAppClass) throws Exception {
    Application app2 = anotherAppClass.newInstance(); 
    Stage anotherStage = new Stage();
    app2.start(anotherStage);
}

N.B.: it wouldn't work if you use special features of standard initialization in anotherApp, e.g. Application.init() or Application.getParameters()

Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • Please, see question http://stackoverflow.com/questions/30445375/javafx-second-application-throws-not-on-fx-application-thread as it's about your solution. –  May 25 '15 at 20:10