I created a project that needs a tomcat server to host a small website. Now I want to add a JavaFX application to this project. This should also start when the Tomcat starts up, but unfortunately nothing happens. FX has been added to the project library.
public class Start {
public static void main( String args[] )
{
try {
Tomcat tomcat = new Tomcat( );
String webappDirectory = new File( "src/main/webapp" ).getAbsolutePath( );
tomcat.setPort( 8080 );
Context context = tomcat.addWebapp( "", webappDirectory );
Tomcat.addServlet( context, "blockchain", new ServletContainer( new Applications( ) ) );
context.addServletMappingDecoded( "/blockchain/api/*", "blockchain" );
tomcat.start( );
tomcat.getServer( ).await( );
ConfigurationGui config = new ConfigurationGui();
Stage stage = new Stage();
config.start(stage);
}
catch ( Exception e )
{
e.printStackTrace( );
}
}
}
Application-Class:
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ConfigurationGui extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = new BorderPane();
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
}
CodePudding user response:
Check to see if the Tomcat await call is never returning. If it never returns, then your JavaFX application will never get initialized. You should probably put these 2 operations in separate threads.
CodePudding user response:
Don't await() at startup
This is what await() does for the tomcat server:
Wait until a proper shutdown command is received, then return.
You state that you want the tomcat server running while your application runs. Therefore you should not await shutdown of the server before you start your JavaFX application.
Use the JavaFX application lifecycle
start()
should not be directly called on the JavaFX app, instead the application launch()
method should be invoked to launch the app. This should be done so that the JavaFX application and system follow the JavaFX application lifecycle as explained in the Application
javadoc.
Probably also move the tomcat initialization code into the app init()
method and store the tomcat server reference so you can later send messages to it (for example to shut it down when it is no longer needed).
If you want to gracefully shutdown the Tomcat server when your app completes, then call the server stop()
method from your application stop()
method. You could await the graceful shutdown of the server after making the call, if you wished to, but doing that in a robust way is beyond the scope of what I will discuss here.
You can probably get rid of your Start
class completely, and just put the main method in the JavaFX application class, and use that to launch your application.
Depending on the quality of the integration you want, you could probably add further logic to nicely synchronize the JavaFX application lifecycle with the Tomcat server Lifecycle, discussing such integration in detail is out of scope for this answer.
Don't reference the src
directory from code
src\main
is a project directory not a runtime location, you should not reference the src
directory directly from your code.