系统无法在JAR中找到指定的路径。

huangapple go评论60阅读模式
英文:

The system cannot find the path specified in JAR

问题

我试图播放一个音频文件。在我的IDE(IntelliJ)中,这完全正常,但在运行JAR文件时,我收到了错误消息java.io.FileNotFoundException: D:\soni801\Documents\Redsea Productions\The Great X Wars\alpha-0.0.7.1.jar\audio\click.au(系统找不到指定的路径)

该文件在JAR文件内的位置是audio/click.au,而JAR文件位于D:\soni801\Documents\Redsea Productions\The Great X Wars\alpha-0.0.7.1.jar,根据我了解,绝对路径应该是D:\soni801\Documents\Redsea Productions\The Great X Wars\alpha-0.0.7.1.jar\audio\click.au,但系统似乎无法在此位置找到文件?发生了什么?

这是我AudioPlayer.java 文件中的代码:

AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath() /*+ "/res"*/ + filePath).getAbsoluteFile());

Clip clip = AudioSystem.getClip();
clip.open(inputStream);

或者,您可以在GitHub存储库上找到整个文件,以及项目的其余部分。

感谢所有的帮助。

我意识到这可能是其他问题的重复,但不幸的是,其他解决方案没有解决我的问题

如果问题不清楚,请提出改进的评论,我将编辑问题

英文:

I'm trying to play an audio file. In my IDE (intellij) that works completely fine, but when running in a JAR, I get the error java.io.FileNotFoundException: D:\soni801\Documents\Redsea Productions\The Great X Wars\alpha-0.0.7.1.jar\audio\click.au (The system cannot find the path specified)

The file's location inside the JAR is audio/click.au, and the JAR is located at D:\soni801\Documents\Redsea Productions\The Great X Wars\alpha-0.0.7.1.jar which afaik should make the absolute path D:\soni801\Documents\Redsea Productions\The Great X Wars\alpha-0.0.7.1.jar\audio\click.au, however the system can't find a file at this location? What's happening here?

This is the code in my AudioPlayer.java file:

AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath() /*+ "/res"*/ + filePath).getAbsoluteFile());

Clip clip = AudioSystem.getClip();
clip.open(inputStream);

Alternatively, you can find the entire file, as well as the rest of the project, on the GitHub repository

All help is appreciated.

I am aware that this might be a duplicate of other questions, but unfortunately other solutions didn't solve my problem

If the question is unclear, comment any improvements I could make, and I will edit the question

答案1

得分: 1

AudioSystem.getAudioInputStream 方法被重载,可以接受一个 File、一个 URL 或一个 InputStream 作为参数。其中,URL 通常是最佳选择。

File 相比的优势在于,URL 可以指向位于 JAR 文件中的文件,而 File 无法做到这一点。

InputStream 相比的优势在于,它会附加一些条件:音频文件必须支持 markreset 方法。根据我的经验,对于是否可能实现这一点,有点靠运气。我承认我不知道具体的原因。

要从您的 JAR 文件中获取 URL,可能最简单的形式如下:

URL url = this.getClass().getResource("yourAudioFileName");

这假设音频资源位于与类 "this" 相同的包中。包的子文件夹也可以这样访问。但我认为不能指望符号 "../" 从父文件夹获取资源。

或者,您可以在包或音频资源的父包中指定一个类。此外,您可以在地址前加上 "/"。在这种情况下,例如 this.getClass().getResource("/yourAudioFileName"),项目的根源文件夹是地址的起点,而不是包含 "this" 的包。

英文:

The AudioSystem.getAudioInputStream method is overloaded and can accept a File, a URL or an InputStream as an argument. Of these, URL is usually the best choice.

It's advantage over File is that a URL can address a file within a jar, which a File cannot do.

It's advantage over InputStream is that additional conditions are imposed: the audio file must support mark and reset methods. My experience with audio files is that it's kind of hit or miss as to whether this will be possible or not. I confess I don't know the specifics as to why.

To obtain a URL from your jar, the simplest form it perhaps the following:

URL url = this.getClass().getResource("yourAudioFileName");

This assumes that the audio resource is in the same package as the class "this". Subfolders of the package can be addressed as well, this way. But I don't think the symbol "../" can be counted on to obtain a resource from a parent folder.

Alternatively, instead of 'this' you can specify a class in the package or parent package of the audio resource. Also, you can prefix the address with a "/". In this last case, e.g., this.getClass().getResource("/yourAudioFileName"), the root source folder of the project is the starting point for the address rather than the package holding 'this'.

huangapple
  • 本文由 发表于 2020年7月22日 01:41:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63020106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定