已经提出的理论的替代理论。通常成功的 getResource() 调用依赖于调用来定位它们的类加载器实例。出于这个原因,我建议使用用户定义对象的实例,从中调用 getResource()。例如
// Sanity check
System.out.println("The value of 'file' is: " + file);
// Presuming kidsClassRoom1 is an instance of kidsClassRoom
AudioInputStream inputStream = AudioSystem.
getAudioInputStream(
kidsClassRoom1.
getClass().
getResourceAsStream("/resources/"+file));
您可能还注意到代码片段使用“/”前缀作为资源。与其他人所说的相反,我相信这意味着资源路径的“从根”,在运行时类路径上的任何 Jar 中找到它。留下“/”或“../”将使类加载器在发生这种情况的类的子路径中搜索资源。
当然 - 确保 Wav 最终出现在 Jar 中!将 .jar 复制/重命名为 .zip 并双击它是在 Windows 上检查存档内容的“快速而肮脏”的方式。