2011-12-28 112 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}

相關問題