代码如下:import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.applet.*;
import java.net.*;
import java.io.*;import javax.swing.JFrame;public class Music extends JFrame {
URL url;
File file;
AudioClip c; public Music() throws MalformedURLException {
file = new File("src\\01.wav");
url = file.toURL();
c = Applet.newAudioClip(url);
this.addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {

System.exit(0);
     c.stop();
}
});
setSize(100, 100);

} public static void main(String[] args) throws MalformedURLException {
Music t = new Music();
t.setVisible(true);
t.c.loop();



}
}目录结构如下:
Test
——src
————default package
——————Music
————01.wav
问题:
在eclipse里面运行没有问题,正常播放音频。可是,用eclipse 的 export  打jar包后 双击能运行,却无法播放音乐。小弟知道是路径问题,可是不会搞,请高手帮忙。补充: 如果有可能请详细解释一下  用  new File(String pathname),在eclipse中 pathname 如何处理。小弟不胜感激!!!

解决方案 »

  1.   

     改为 file = new File("./src\\01.wav");
    看看
      

  2.   

    file = new File(filePath);
    按相对路径,这个String的filePath就是相对于当前目录的路径(src同级)。
    file = new File("01.wav");
    --src
    --01.wav
    file = new File("test\\01.wav");
    --src
    --test
    ---01.wav
      

  3.   

    这样来获取哦
    Thread.currentThread().getContextClassLoader().getResourceAsStream("t.txt");
      

  4.   

    Thread.currentThread().getContextClassLoader().getResource("01.wav")
    返回的就是URL对象