9

I am trying to embed a Jetty server in an application and am seeing a really strange problem:

According to documentation, a simple server (which I am building as a test) can be started up using the following code:

import org.eclipse.jetty.server.Server;

public class SimpleServer throws Exception
{

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

      server.start();
      server.join();
   }

}

I believe I have the correct Jar file from the downloaded Jetty:

jetty-server-9.3.7.v20160115.jar

Unfortunately, I am seeing that the Server class I am using has no public start() method. It has a protected start() method that has a LifeCycle parameter, but that is it. The public start() method referenced in the documentation (and in several answers here in Stack Overflow) does not exist!

Am I using the right Server class? If not, where do I get a proper one???

Someone please advise...

Factor Three
  • 2,094
  • 5
  • 35
  • 51

4 Answers4

7

You need also add "jetty-util-{version}" into your project and this will fix your issue. Or you can use maven in your project and add "jetty-server-{version}" as maven dependency. Maven will automatically download all jetty-server relative jars (including jetty-util-{version}).

rgolovakha
  • 518
  • 2
  • 5
  • 17
  • 1
    I don't understand how this will help. I have both jars in my dependencies and the method is still protected. I can't find any examples that acknowledge the issue at all. – mdhirsch May 01 '17 at 17:08
  • Once you add this dependency - it is going to use AbstractLifeCycle.start method. If you added "jetty-util-{version}" - refresh your project and now public Server.start method without params should be available. – rgolovakha May 07 '17 at 13:30
  • Worked for me. Thanks. I've been away from Java for a while and, as far as I recall, there was no such thing as extension method (like C# has). This answer would be awesome if the mechanics behind it was explained. – user666412 May 30 '17 at 22:33
  • found my own answer: AbstractLifeCycle is far away in the inheritance chain. – user666412 May 30 '17 at 22:37
1

There's no problem here.

A public modifier Server.start() exists, and has existed since (at least) Jetty 5.0 days.

Not sure what library / build you are using, but this method is not protected, in the Server class, the AbstractLifeCycle (abstract) class or even the LifeCycle interface.

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • In Jetty 9.2.14 it is definitely protected. Here is some slightly older javadoc that also shows it as protected http://download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/server/Server.html – mdhirsch May 01 '17 at 17:06
  • @mdhirsch `start(LifeCycle)` (protected/internal) is not the same as `start()` (public/api). – Joakim Erdfelt May 01 '17 at 17:17
0
public abstract interface org.eclipse.jetty.util.component.LifeCycle

is in jetty-util-{version}.jar, add this jar to your project build path

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
yqbjtu
  • 26
  • 3
-1

Server.start() is protected. If you write a MyJetty class which does nothing but extend he jetty Server, and it is an inner class, you can call the start() method on it.

Here's what I did in the class that wanted to start Jetty()

/**
 * Extend the jetty server just so you can call the protected method start()
 */
static class MyJettyServer extends Server {

    public MyJettyServer(int port) {
        super(port);
    }
}

This seems like very much the wrong answer, but it worked.

mdhirsch
  • 861
  • 7
  • 7