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