2015-08-28 72 views
0

我已成功在Android上部署jetty 7嵌入式服務器問題是我無法將其設置爲訪問我的文件夾jar文件(jar文件在PC上完美運行,並具有自包含的依賴關係,它在android上作爲外部庫添加)。我希望將此文件夾設置爲resourceBase我的嵌入式web應用程序jetty在Android中的外部Java庫(又名jar文件)中設置資源庫

這裏是完整的代碼

  System.out.println(" server init attempt on port:"+curPort); 
      ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); 
      Server server = new Server(curPort); 
      server.setHandler(context); 
      context.setContextPath("/");   
      System.out.println(" servlet main initing"); 
      DSSServer servlet = new DSSServer(); 
      context.addServlet(new ServletHolder(servlet), "/DSS"); 
      ResourceHandler res = new ResourceHandler(); 
      System.out.println(" getting resBase:"); 
      //[code above works fine] 

      //[!ATTENTION HERE!] 
      String resDir = "/dss_core/HTML5/webapp"; 
      //This runs on PC fine but creates a null pointer exception in Android 
      String resurl = DSSServer.class.getResource(resDir).toExternalForm(); 
      //Here is another non-working varient 
      //String resurl = servlet.getClass().getClassLoader().getResource(resDir).toExternalForm(); 

      //[code below works fine] 
      res.setResourceBase(resurl); 
      res.setDirectoriesListed(true); 
      context.setHandler(res); 
      System.out.println(" servlet main inited"); 

這是代碼塊,使空指針異常

  String resDir = "/dss_core/HTML5/webapp"; 
      //This runs on PC fine but creates a null pointer exception in Android 
      String resurl = DSSServer.class.getResource(resDir).toExternalForm(); 
      //Here is another non-working variant 
      //String resurl = servlet.getClass().getClassLoader().getResource(resDir).toExternalForm(); 

在Android中訪問外部java庫資源文件夾的正確方法是什麼?我知道Android有它自己的資源管理系統,但是我不能將本地Android代碼放到我的外部Java庫中,因爲它是專門用於PC的jar文件。

的替代方法我可以做的是提取我的資源庫到本地文件夾,但它留下噸的臨時文件

我寧願不做

BTW Android有沒有問題,在同一時間訪問一個文件從我的Java的lib裏面

  //Accessing individual files work on Android 
      InputStream input = DatabaseManager.class.getResourceAsStream("/h2db.mv.db"); 
+0

在你試圖在Android上使用它之前,你有沒有'dex'-ify你的外部Java庫? (或者它現在是'art'-ify嗎?) –

+0

是的,只有jetty7可以是dexed,在IOS(roboVM)中也有同樣的成功事例,jetty 8和9不能被排除,所以我被jetty7 – jyonkheel

+0

看起來像我會回答我自己的問題,我會稍後更新 – jyonkheel

回答

0

看來這樣是不可能的,我結束了在安卓(如/mnt/sdcard/.myApp/temp)某處提取文件和手動複製每一個文件有

String resDir = "/dss_core/HTML5/webapp"; 
String resurl = dss_core.util.DRYcode.platformIsAndroid()?"file:/mnt/sdcard/protected/"+servletName: 
       DRYcodeWeb.class.getResource(resDir).toString(); 
if (dss_core.util.DRYcode.platformIsAndroid()) 
    for (String str:resourceList) 
     DRYcode.copy_("/dss_core/HTML5/webapp/"+str,"/mnt/sdcard/protected/"+servletName+"/"+str);