简体   繁体   English

通过 .jar 部署 Jetty 服务器。 为什么我无法访问索引?

[英]Deploying Jetty server via .jar. Why can't I access the index?

I'm trying to deploy a Jetty server from a jar file.我正在尝试从 jar 文件部署 Jetty 服务器。 When jar is being run on the server, it reaches the Jetty 404 page at least, but is unable to reach index.html . jar 在服务器上运行时,它至少会到达 Jetty 404 页面,但无法到达index.html

My main class to launch the server looks like this, and works fine locally when run through the IDE on localhost:我用于启动服务器的主类如下所示,并且在本地主机上通过 IDE 运行时在本地运行良好:

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

    ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
    servletContextHandler.setContextPath("/");

    DefaultServlet defaultServlet = new DefaultServlet();
    ServletHolder holderPwd = new ServletHolder("default", defaultServlet);

    final URL htmlDirectory = JerseyApplication.class.getResource("/html");

    holderPwd.setInitParameter("resourceBase", htmlDirectory.getFile());

    servletContextHandler.addServlet(holderPwd, "/*");
    server.setHandler(servletContextHandler);

    ServletHolder servletHolder = servletContextHandler.addServlet(ServletContainer.class, "/api/*");
    servletHolder.setInitOrder(0);
    servletHolder.setInitParameter(
            "jersey.config.server.provider.packages",
            "com.x.y.z.parser");

    try {
        LOGGER.info("Starting server");
        server.start();
        server.join();
    }
    catch (Exception ex) {
        LOGGER.error("Server failed to start - Aborting");
        ex.printStackTrace();
    }
    finally {
        LOGGER.info("Destroying server");
        server.destroy();
    }
}

All html stuff is in a the src/main/resources/html directory.所有 html 内容都在src/main/resources/html目录中。

When I run jar tvf jarfile.jar | grep html当我运行jar tvf jarfile.jar | grep html jar tvf jarfile.jar | grep html I can see the html directory and it's contents are in there: jar tvf jarfile.jar | grep html我可以看到html目录,它的内容在那里:

0 Thu Nov 01 11:48:46 UTC 2018 html/
2258 Thu Nov 01 11:48:46 UTC 2018 html/formRequest.js
871 Thu Nov 01 11:48:46 UTC 2018 html/index.html

Thanks!谢谢!

Use the URL you got from htmlDirectory as the Base Resource for the entire ServletContextHandler .使用从htmlDirectory获得的 URL 作为整个ServletContextHandler的基本资源。

See prior answer for details: https://stackoverflow.com/a/39019797/775715有关详细信息,请参阅先前的答案: https : //stackoverflow.com/a/39019797/775715

final URL htmlDirectory = JerseyApplication.class.getResource("/html");

// TODO: Handle error if htmlDirectory == null

ServletContextHandler servletContextHandler = new ServletContextHandler(NO_SESSIONS);
servletContextHandler.setContextPath("/");
servletContextHandler.setBaseResource(Resource.newResource(htmlDirectory));

DefaultServlet defaultServlet = new DefaultServlet();
ServletHolder holderPwd = new ServletHolder("default", defaultServlet);
// holderPwd.setInitParameter("resourceBase", htmlDirectory.getFile()); <-- not needed here
servletContextHandler.addServlet(holderPwd, "/"); // NOTE: MUST be "/" not "/*"!

One last thing, you seem to be using Jersey.最后一件事,您似乎在使用 Jersey。 (aka jersey.config.server.provider.packages ) Make sure you DISABLE the Jersey configurations that make Jersey serve static content itself. (又名jersey.config.server.provider.packages )确保禁用使 Jersey 本身提供静态内容的 Jersey 配置。 Let Jetty be responsible.让码头负责。 (as to how this is done, that's another question, which is Jersey version specific and has answers already on stackoverflow) (至于这是如何完成的,这是另一个问题,这是特定于 Jersey 版本的,并且已经在 stackoverflow 上提供了答案)

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

相关问题 部署.jar文件:为什么我不能加载图标文件? - Deploying .jar file: Why can't I load icon files? Jetty客户端无法通过HTTP / 2连接到Jetty服务器 - Jetty client can't connect to Jetty server via HTTP/2 Jetty Server 启动但无法在浏览器上访问 - Jetty Server started but can't access it on browser 无法通过URL访问.jar中的文件 - Can't access file in .jar via URL 无法在Firefox 50上通过SSL连接到Jetty 9服务器 - Can't connect to Jetty 9 server via SSL with Firefox 50 为什么不能通过公网ip访问服务器 - Why can't access server via public ip 为何运行该文件时,除非我在Jar所在的文件夹中,否则为什么我无法访问其文件? - Why can't I access a file within my jar unless I'm in the folder with the Jar when I run it? 将 WAR 文件部署到 Jetty:服务器正在运行,但应用程序未启动 - Deploying WAR file to Jetty: server is running, but application didn't start 我想在GAE / J上使用OpenID。我不想要另外的+ 100kB JAR。我迷路了吗? - I want OpenID on GAE/J. I don't want yet another +100kB JAR. Am I lost? 我们在哪里可以下载oracle.xml.diff软件包jar。 我需要导入我的Java程序 - where can we download the oracle.xml.diff package jar. I need to import in a java program of mine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM