在Java应用程序中嵌入码头并导出为jar [英] Embed jetty in java app and export as jar

查看:88
本文介绍了在Java应用程序中嵌入码头并导出为jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个嵌入Jetty Web服务器的Java应用程序,该服务器又提供了使用Google Web Toolkit开发的内容.在Eclipse中运行时,一切正常,但是当我将其导出为jar文件时,得到的只是一条Jetty错误消息,提示找不到文件".

I'm making a java application which embeds a Jetty web server, which in turn serves content developed with Google Web Toolkit. It's all working fine when run in Eclipse, but when I export it as a jar file all I get is a Jetty error message saying "File not found".

Jetty服务器是这样启动的:

The Jetty server is launched like this:

    WebAppContext handler = new WebAppContext();
    handler.setResourceBase("./war");
    handler.setDescriptor("./war/WEB-INF/web.xml");
    handler.setContextPath("/");
    handler.setParentLoaderPriority(true);
    server.setHandler(handler);

    try {
        server.start();
        server.join();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我怀疑问题是在handler.setResourceBase()和handler.setDescriptor()中使用的相对路径.我已经用谷歌搜索并测试了许多解决方案,但到目前为止都无济于事.特别是我尝试使用类似getClass().getResource("./war").toExternalForm()的方法,但这只会引发Null异常.

I suspect that the problem is the relative paths used in handler.setResourceBase() and handler.setDescriptor(). I've googled and tested lots of solutions to this but so far to no avail. Particularly I've tried using something like getClass().getResource("./war").toExternalForm() but this just throws Null exceptions.

我也尝试过:

ProtectionDomain protectionDomain = Start.class.getProtectionDomain();
URL location = protectionDomain.getCodeSource().getLocation();

但这只会导致Jetty提供Java类的目录列表.

but that only results in a Jetty serving a directory listing of the java classes.

有什么办法吗?

推荐答案

  1. 将已编译的GWT应用程序的所有文件复制到您的Java软件包之一中.例如:

  1. Copy all files of the compiled GWT application into one of your Java packages. For instance:

my.webapp.resources
  html/MyPage.html
  gwtmodule/gwtmodule.nocache.js
  ...

(htmlgwtmodule也将成为Java软件包).

(html, gwtmodule will become Java packages as well).

配置嵌入式Jetty实例以提供这些文件.

Configure the embedded Jetty instance to serve these files.

 final String webDir = this.getClass().
       getClassLoader().getResource("my/webapp/resources").toExternalForm();

 final Context root = new Context(server, "/", Context.SESSIONS);
 root.setContextPath("/");
 root.setResourceBase(webDir);
 root.addServlet(MyGwtService.class, "/servlets/v01/gwtservice"); 

无论是从Eclipse中运行应用程序还是从Eclipse部署应用程序,这种方法都对我有效.

This approach works for me both when the application is run from eclipse as well as when it is deployed.

这篇关于在Java应用程序中嵌入码头并导出为jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆