1

I do want to run a main class which deploys a jetty server with the content of my vaadin application.

final Server server = new Server();
     final Connector connector = new ServerConnector( server );
     (( AbstractNetworkConnector ) connector).setPort( 3131 );
     server.setConnectors( new Connector[] { connector } ); //
     final WebAppContext webappcontext = new WebAppContext();
     final File warFile = new File( "target/avx-gcms-1.0.war" );
     webappcontext.setClassLoader( Thread.currentThread().getContextClassLoader() );
     webappcontext.setContextPath( "/" );
     webappcontext.setWar( warFile.getAbsolutePath() );
     server.setHandler( webappcontext );
     server.start();
     System.out.println( "Jetty started, please go to http://localhost:" + 3131 + "/" );

Here is my code for the main method but when I do run this, it works without an error but I do get this in m localhost.

This is my workspace

I have seen some related questions to this yet they were not very helpful. Thank you if you help in advance.

Bleach
  • 309
  • 3
  • 18
  • How do you deploy the jetty server? Isn't it simpler to just include your war in the complete jetty package, instead of pushing it later on? – André Schild Mar 29 '18 at 14:47
  • I do maven build with the goal jetty:run, this is how I run it now. I want to create a file like .bat or .launch, trigger the main class that runs the jetty. I am afraid I couldn't understand how could I include my war in complete jetty package. @AndréSchild – Bleach Mar 29 '18 at 15:08
  • Possible duplicate of https://stackoverflow.com/a/37553587/775715 – Joakim Erdfelt Mar 29 '18 at 16:01

1 Answers1

1

You might want to do it the other way around: instead of war file, make the application as runnable jar directly.

You need to have this main class to the project along with your Vaadin UI class, and wire it up directly to Jetty:

public static void main(String[] args) {
    Server server = new Server(3131);

    ServletContextHandler contextHandler
            = new ServletContextHandler(ServletContextHandler.SESSIONS);
    contextHandler.setContextPath("/");

    ServletHolder sh = new ServletHolder(new VaadinServlet());
    contextHandler.addServlet(sh, "/*");
    contextHandler.setInitParameter("ui", HelloWorldUI.class.getCanonicalName());
    server.setHandler(contextHandler);

    try {
        server.start();
        server.join();

    } catch (Exception ex) {
        Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Then configure maven-shade-plugin and exec-maven-plugin in your pom.xml to create a runnable jar file like in here.

Check out that project for full example, but note that the setup is bit different with externalised static resources (widgetset and css), but you can skip that configuration in your case.

eeq
  • 2,108
  • 12
  • 21
  • This uses the `ServletContextHandler`, a very common technique that avoid much of the complexity of a `WebAppContext`. For another uber-jar example, see https://github.com/jetty-project/embedded-jetty-uber-jar – Joakim Erdfelt Mar 30 '18 at 11:28