2014-05-11 79 views
-1

package soundcliptest;列出jar文件的內容

// development environment(NetBeans 8.0) 
// 
// NetBeansProjects 
//  SoundClipTest 
//   Source Packages 
//    resources 
//    ding.wav 
//    soundcliptest 
//    SoundClipTest.java 
// 

// unZipped jar file 
// 
// SoundClipTest 
// META-INF 
// resourses 
//  ding.wav 
// soudcliptest 
//  SoundClipTest.class 
// 

我還在學習如何使用這個工具,夥計們。我似乎無法獲得他們所屬的進口產品。

我需要知道如何從代碼中查看jar文件。 File方法不能破解它。必須有一些方法來查找資源「目錄」的內容。我想要做的是製作/ resources /下的聲音文件菜單。我可以讓它在開發環境中工作,但不能從jar文件中獲得。也許一些'zip'方法?但我沒有找到他們。提前致謝。

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.io.*; 
import java.net.URL; 
import javax.sound.sampled.*; 
import javax.swing.*; 
import javax.swing.border.BevelBorder; 

public class SoundClipTest extends JFrame { 

    JTextPane myPane; 
    String title; 
    String showIt; 

    public SoundClipTest() { 

     // get something to write on 
     this.myPane = new JTextPane(); 
     this.myPane.setPreferredSize(new Dimension(700, 100)); 
     this.myPane.setBorder(new BevelBorder(BevelBorder.LOWERED)); 

     try { 
      // Open an audio input stream. 
      URL dingUrl = getClass().getResource("/resources/ding.wav"); 

      // show the path we got 
      String path = dingUrl.getPath(); 

      //trim 'ding.wav' from file path to get a directory path 
      String dirPath = path.substring(0, path.length()-"ding.wav".length()); 

      // now get a Url for the dir from getResource and show THAT path 
      URL dirUrl = getClass().getResource("/resources/"); 
      String urlPath = dirUrl.getPath(); 

      // the dirUrl path is just like the trimmed 'ding' file path 
      // so use urlPath to get a file object for the directory 
      try { 
       File f = new File(urlPath); //works fine in dev environment 
       String filePath = f.getPath(); // but not from jar file 
       title = f.list()[0]; // from jar, null pointer exception here 

       // whan things go right (HA HA) we display this 
       showIt = ("    >>>>> IT WORKED!! <<<<<!" 
         + "\n path to file:  "+ path 
         + "\n path to dir:  " + dirPath 
         + "\n path from URL: " + urlPath 
         + "\n path from file: "+ filePath + "\n " + title); 

      } catch (Exception e) { 
       // you get this on display when executing the jar file 
       showIt = ("   PHOOEY" 
         + "\n the ding " + path 
         + "\n trimmed path " + dirPath 
         + "\n the URL path " + urlPath 
         + "\n could not create a File object from path"); 

       // the stack trace shows up only if you run from the terminal 
       e.printStackTrace(); 
      } 

      // We get a nice little 'ding', anyway 
      AudioInputStream ais = AudioSystem.getAudioInputStream(dingUrl); 
      Clip clip = AudioSystem.getClip(); 
      clip.open(ais); 
      clip.start(); 

      //but nuttin else good - show the disapointing results 
      myPane.setText(showIt); 

     } catch (UnsupportedAudioFileException | LineUnavailableException | IOException e) { 
      e.printStackTrace(); 
     } catch (NullPointerException e) { 
      System.out.println("Ouch!!! Damn, that hurt!"); 
      e.printStackTrace(); 
     } 
    } 

    public static void main(String[] args) { 
     SoundClipTest me = new SoundClipTest(); 
     JFrame frame = new JFrame("Sound Test"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(me.myPane, BorderLayout.CENTER); 
     frame.pack(); 
     frame.setVisible(true); 
    } 
} 
+0

嗯...有沒有什麼地方我能找到的TI如何使用這個工具的解釋?或者我只是繼續試驗。猜猜我可以自己弄清楚,如果我必須的話。 – user3625050

回答

0

OK,不是一個完整的答案,但因爲這部分不是很好的記錄(Oracle的上頁已經過時了),這裏是如何用在一個jar文件的ZIP文件系統:

final Path pathToJar = Paths.get(...).toRealPath(); 
final String jarURL = "jar:file:" + pathToJar; 
final Map<String, String> env = new HashMap<>(); 

final FileSystem jarfs = FileSystems.newFileSystem(URI.create(jarURL), env); 

final Path rootPath = jarfs.getPath("/resources"); 

然後,您可以使用Files.newDirectoryStream()而不是rootPath來獲取jar中「目錄」內的文件列表。如果要遞歸列表,請使用Files.walkFileTree()並編寫自己的FileVisitor(大部分時間,擴展SimpleFileVisitor就足夠了)。

備註:a FileSystem implements Closeable;一旦你完成了它,你應該確保到.close()。對於這個問題,DirectoryStream也是如此。

0

買者自負

這是假設你沒有用類加載器把玩,這將與URLClassLoader工作,但就是類的實現細節,而不是公共API的一部分。

如果你加載一個classpath目錄資源,那麼你會得到該目錄中每個資源的一行。

讓我們假設你有這樣的結構:

/resources 
    /test1.txt 
    /test2.txt 

如果我們執行下列操作:

try (final Scanner scanner = new Scanner(getClass().getResourceAsStream("/resources"))) { 
    while (scanner.hasNextLine()) { 
     final String line = scanner.nextLine(); 
     System.out.println(line); 
    } 
} 

這將輸出:

test1.txt 
test2.txt 

所以,你可以用它來返回文件名稱列表:

List<String> readClassPath(final String root) { 
    final List<String> resources = new LinkedList<>(); 
    try (final Scanner scanner = new Scanner(getClass().getResourceAsStream(root))) { 
     while (scanner.hasNextLine()) { 
      final String line = scanner.nextLine(); 
      System.out.println(line); 
      resources.add(root + "/" + line); 
     } 
    } 
    return resources; 
} 

這將返回:

[/resources/test1.txt, /resources/test2.txt]