简体   繁体   English

Jetty 9 Server没有start()方法

[英]Jetty 9 Server has no start() method

I am trying to embed a Jetty server in an application and am seeing a really strange problem: 我试图在应用程序中嵌入Jetty服务器,并看到一个非常奇怪的问题:

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中获得了正确的Jar文件:

jetty-server-9.3.7.v20160115.jar 码头 - 服务器9.3.7.v20160115.jar

Unfortunately, I am seeing that the Server class I am using has no public start() method. 不幸的是,我发现我使用的Server类没有public start()方法。 It has a protected start() method that has a LifeCycle parameter, but that is it. 它有一个受保护的start()方法,它有一个LifeCycle参数,但就是这样。 The public start() method referenced in the documentation (and in several answers here in Stack Overflow) does not exist! 文档中引用的public start()方法(以及Stack Overflow中的几个答案)不存在!

Am I using the right Server class? 我使用正确的服务器类吗? If not, where do I get a proper one??? 如果没有,我在哪里得到一个合适的???

Someone please advise... 有人请指教......

You need also add "jetty-util-{version}" into your project and this will fix your issue. 您还需要在项目中添加“jetty-util- {version}”,这将解决您的问题。 Or you can use maven in your project and add "jetty-server-{version}" as maven dependency. 或者您可以在项目中使用maven并添加“jetty-server- {version}”作为maven依赖项。 Maven will automatically download all jetty-server relative jars (including jetty-util-{version}). Maven将自动下载所有jetty-server相关jar(包括jetty-util- {version})。

There's no problem here. 这里没问题。

A public modifier Server.start() exists, and has existed since (at least) Jetty 5.0 days. 公共修饰符Server.start()存在,并且自(至少)Jetty 5.0天起就存在。

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. 不确定您正在使用哪个库/构建,但此方法不受保护,在Server类, AbstractLifeCycle (抽象)类甚至LifeCycle接口中。

public abstract interface org.eclipse.jetty.util.component.LifeCycle

jetty-util-{version}.jar ,将此jar添加到项目构建路径中

Server.start() is protected. Server.start()受到保护。 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. 如果你编写一个除了扩展jetty Server之外什么都不做的MyJetty类,并且它是一个内部类,你可以在其上调用start()方法。

Here's what I did in the class that wanted to start Jetty() 这是我在课堂上想要启动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. 这似乎是错误的答案,但它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM