2012-06-26 37 views
1

即時通訊嘗試從使用java-webstart下載的jar文件中提取一些文件。 下面的代碼被用於定位的jar和啓動文件系統從JWS下載的jar文件中提取一些內容

1 final ProtectionDomain domain = this.getClass().getProtectionDomain(); 
2 final CodeSource source = domain.getCodeSource(); 
3 final URL url = source.getLocation(); 
4 final URI uri = url.toURI(); 
5 Path jarPath = Paths.get(uri); 
6 
7 FileSystem fs = FileSystems.newFileSystem(jarPath, null); 

也能正常工作時的jar文件是一個本地磁盤上,但在JWS場景5號線失敗,因爲

日誌說:URL = HTTP://本地主機:8080/myJarFile.jar中

java.nio.file.FileSystemNotFoundException: Provider "http" not installed 
at java.nio.file.Paths.get(Unknown Source) 

如果我理解正確的JWS,myJarFile.jar中已經下載到一些緩存了,所以實施FileSystemProvider爲HTTP得到一些內容從myjarfile.jar看起來很慢並且compl icated。那麼有關如何繼續的好主意?

回答

1

日誌說:URL = HTTP://本地主機:8080/myJarFile.jar中

這是由Sun做出安全決策之前,甲骨文收購他們。他們認爲這不適用於小應用程序或JWS應用程序。要知道本地文件系統上資源的位置,所以返回的URI現在總是會指向服務器,即使它們在本地緩存在應用程序中也不例外。有all-permissions安全級別。

那麼,如何進行任何好主意?

重新設計應用程序。這是唯一的實際解決方案。

有許多方法可以爲內容迭代Zip或Jar,但最簡單的方法是將內容列表包含在Jar的已知位置中,使用getResource()找到它,然後讀取它,然後提取每個資源。

+1

感謝您的回答。我試圖將帶有一些內容的.jar/zip嵌入到myjarfile.jar中。然後使用您的建議將其提取到光盤上的已知位置。然後保留我目前的解壓縮文件。 –

+0

很酷。希望它進展順利。 :) –

1

下面是你的想法安德魯的實現,它利用了DirUtil包我發現這裏: http://codingjunkie.net/java-7-copy-move/

public class Zipper { 

    private static final String TEMP_FILE_PREFIX = "temp-"; 
    private static final String TEMP_FILE_SUFIX = ".jar"; 

    private Logger logger = Logger.getLogger(getClass().getName()); 

    public Path extractProgram(String locationOfEmbeddedJar, String installDir) { 
     Path installPath = null; 
     try { 
      installPath = Paths.get(installDir); 
      if (Files.isDirectory(installPath)) { 
       logger.warn("program already installed"); 
      } else { 
       installPath = Files.createDirectory(installPath); 
       Path tempJar = Files.createTempFile(TEMP_FILE_PREFIX, 
         TEMP_FILE_SUFIX); 
       this.extractEmbeddedJar(locationOfEmbeddedJar, tempJar.toFile()); 

       logger.warn("in jarfile"); 
       // in jar file 
       FileSystem fs = FileSystems.newFileSystem(tempJar, null); 
       Path programPath = fs.getPath("/"); 

       logger.warn("programPath=" + programPath + " fileSystem=" 
         + programPath.getFileSystem()); 

       DirUtils.copy(programPath, installPath); 
      } 
     } catch (IOException e) { 
      logger.warn(e); 
     } 

     return (installPath); 

    } 

    private void extractEmbeddedJar(String locationOfEmbeddedJar, 
      File locationOfTargetJar) { 
     logger.warn("extractEmbeddedJar() " + locationOfEmbeddedJar); 
     ClassLoader loader = this.getClass().getClassLoader(); 

     InputStream inputStream = loader 
       .getResourceAsStream(locationOfEmbeddedJar); 
     try { 
      OutputStream out = new FileOutputStream(locationOfTargetJar); 
      byte buf[] = new byte[1024]; 
      int len; 
      while ((len = inputStream.read(buf)) > 0) { 
       out.write(buf, 0, len); 
      } 
      out.close(); 
      inputStream.close(); 

     } catch (IOException e) { 
      logger.warn(e); 
     } 
    } 
} 
+0

偉大的東西! :)發佈解決方案。我注意到你刪除了'剔',然後重新應用到我的答案。如果您將自己的答案標記爲已接受的答案,那也可以。 :) –