Jetty 9 Server has no start() method
Asked Answered
D

4

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...

Depositor answered 1/3, 2016 at 22:58 Comment(0)
P
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}).

Politicize answered 27/3, 2016 at 18:4 Comment(4)
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.Effluent
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.Politicize
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.Brehm
found my own answer: AbstractLifeCycle is far away in the inheritance chain.Brehm
M
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.

Madera answered 2/3, 2016 at 16:27 Comment(2)
In Jetty 9.2.14 it is definitely protected. Here is some slightly older javadoc that also shows it as protected download.eclipse.org/jetty/stable-9/apidocs/org/eclipse/jetty/…Effluent
@Effluent start(LifeCycle) (protected/internal) is not the same as start() (public/api).Madera
S
0
public abstract interface org.eclipse.jetty.util.component.LifeCycle

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

Secret answered 6/2, 2018 at 2:14 Comment(0)
E
-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.

Effluent answered 1/5, 2017 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.