2011-12-28 111 views
3

我正在制作一个嵌入Jetty Web服务器的Java应用程序,该服务器反过来提供使用Google Web Toolkit开发的内容。在Eclipse中运行时,它都工作正常,但是当我将它作为jar文件导出时,我得到的是一条Jetty错误消息,指出“未找到文件”。在java应用程序中嵌入jetty并将其导出为jar

码头服务器启动这样的:

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(),但这只是抛出空例外。

我也试过:

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

但只导致码头服务Java类的目录列表。

有没有办法做到这一点?

回答

1
  1. 将编译后的GWT应用程序的所有文件复制到一个Java包中。例如:

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

    htmlgwtmodule将成为Java包为好)。

  2. 配置嵌入式Jetty实例来提供这些文件。

    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中运行,以及当它被部署。

0
+0

如果你知道答案,请在这里更详细地描述它。提供一些提取物。该链接可能会过时... – 2013-06-20 07:53:56

+0

@RadimKöhler这似乎是一个非问题,“如果你知道答案”部分不满足的链接。它甚至不会在嵌入式Jetty服务器中远程引用部署WAR。 – cklab 2013-07-10 22:21:38

0

它看起来像ClassLoader.getResource方法,不明白一个空字符串或。或/作为参数。在我的jar文件中,我必须将所有的stuf移动到WEB-INF。因此,代码看起来像

`contextHandler.setResourceBase(EmbeddedJetty.class.getClassLoader().getResource("WEB-INF").toExternalForm());` 

这样的背景下看起来是这样,那么:

的ContextHandler:744 - 入门oejwWebAppContext @ 48b3806 {/,JAR:文件:/用户/ XXX /项目/保管箱/ UI /target/ui-1.0-SNAPSHOT.jar!/WEB-INF,AVAILABLE}

相关问题