系统找不到JAR中指定的路径 [英] The system cannot find the path specified in JAR

查看:150
本文介绍了系统找不到JAR中指定的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试播放音频文件.在我的IDE(intellij)中可以正常工作,但是在JAR中运行时,出现错误 java.io.FileNotFoundException:D:\ soni801 \ Documents \ Redsea Productions \ Great X Wars \ alpha-0.0.7.1.jar \ audio \ click.au(系统找不到指定的路径)

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)

文件在JAR中的位置为 audio/click.au ,并且JAR位于 D:\ soni801 \ Documents \ Redsea Productions \ Great X Wars \ alpha-0.0.7.1.jar ,哪个afaik应该将其作为绝对路径 D:\ soni801 \ Documents \ Redsea Productions \ Great X Wars \ alpha-0.0.7.1.jar \ audio \ click.au ,但是系统无法在此位置找到文件?这是怎么回事?

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?

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

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);

或者,您可以在感谢所有帮助.

我知道这可能是其他问题的重复,但不幸的是其他解决方案并不能解决我的问题

如果问题不清楚,请评论我可以做的任何改进,然后我将对问题进行编辑

推荐答案

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

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.

File 相比,优点是 URL 可以寻址jar中的文件,而 File 则不能.

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

InputStream 相比,它的优势在于附加了附加条件:音频文件必须支持 mark reset 方法.我在音频文件方面的经验是,无论是否可行,都会有一定的成败.我承认我不知道具体原因.

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.

要从jar中获取URL,最简单的形式可能是以下形式:

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

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

这假定音频资源与类"this"在同一包中.包的子文件夹也可以通过这种方式寻址.但是我不认为符号"../"是"../".可以依靠它来从父文件夹获取资源.

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.

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

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'.

这篇关于系统找不到JAR中指定的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆