0

This is a 100% win console application. So here's the problem.

I want to load the file music.xm that I want to place inside the jar. The problem come up when I try to call the file through a relative path. The start directory it's not the Java project one, but my Windows User Folder.

If I call

File music = new File("\\music.xm");


javax.sound.sampled.UnsupportedAudioFileException: /C:/Users/XXXX/Desktop/./music/music.xm

If I call

    File music = new File(".\\music.xm");

I get

    javax.sound.sampled.UnsupportedAudioFileException: /C:/music.xm
4

1 回答 1

1

如果它在你的罐子里,你可以使用

getclassLoader().getResourceAsStream("music.xm")

你可以随意使用这个 inputStream。但请记住,路径应该相对于类加载器的类路径根。

此外,如果您确定“music.xm”作为independent file on filesystem.class 文件的固定相对位置存在,您还可以使用:

getclassLoader().getResource("music.xm")

您可以查看SO和此处的文档

于 2013-08-08T12:49:40.367 回答