我应该将 Java 程序的资源放在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21056815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:14:28  来源:igfitidea点击:

Where should I put my resources for a Java program?

javaeclipseresourcesprojectembedded-resource

提问by user3150201

I'm programming in Java using Eclipse.

我正在使用 Eclipse 用 Ja​​va 编程。

I have recently tried playing a sound file in my program. I did this:

我最近尝试在我的程序中播放声音文件。我这样做了:

URL url = this.getClass().getResource("ih.wav");
audioIn = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();

This only worked, when I tried to put ih.wav in the bin folder, as if the bin folder was the "base folder" for my project. Putting the file in the main folder of the project, didn't work. Putting it in the src folder, didn't work too.

这仅在我尝试将 ih.wav 放在 bin 文件夹中时才起作用,就好像 bin 文件夹是我项目的“基本文件夹”一样。将文件放在项目的主文件夹中,没有用。把它放在 src 文件夹中,也没有用。

Can someone explain to me where to put my resources for Java programs? Also, does it matter if I import the resources into Eclipse? Thanks

有人可以向我解释将我的 Java 程序资源放在哪里吗?另外,如果我将资源导入 Eclipse 有关系吗?谢谢

EDIT:

编辑:

Tried to create a resources folder in the main project folder, still gives me a NullPointerException:

试图在主项目文件夹中创建一个资源文件夹,仍然给我一个 NullPointerException:

        URL url1 = this.getClass().getResource("res/ah.wav");
        audioIn = AudioSystem.getAudioInputStream(url1);
        clip1 = AudioSystem.getClip();
        clip1.open(audioIn);

        URL url2 = this.getClass().getResource("res/eh.wav");
        audioIn = AudioSystem.getAudioInputStream(url2);
        clip2 = AudioSystem.getClip();
        clip2.open(audioIn);

        URL url3 = this.getClass().getResource("res/ih.wav");
        audioIn = AudioSystem.getAudioInputStream(url3);
        clip3 = AudioSystem.getClip();
        clip3.open(audioIn);

        clip1.start();
        clip2.start();
        clip3.start();

采纳答案by JamoBox

Best practice would be to create a folder (depending on your package structure) that is called either resor resourceswhere you will keep project related resources such as any images or sound files.

最佳做法是创建一个文件夹(取决于您的包结构),该文件夹被称为resresources您将保存项目相关资源(例如任何图像或声音文件)的位置。

Depending on preference, either put this folder in your main project folder MyProject/resor put it inside the relevant package, e.g. main.java.res

根据偏好,将此文件夹放在主项目文件夹中MyProject/res或将其放在相关包中,例如main.java.res

EDIT

编辑

To adhere more to your question, I'm not too familiar with the getResources()method, so instead I would personally recommend using some code similar to:

为了更多地坚持您的问题,我对该getResources()方法不太熟悉,因此我个人建议使用一些类似于以下内容的代码:

File file = new File("ih.wav");
audioIn = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();

That shouldproduce the results you are looking for, given that you change the path of he file accordingly (e.g. "main/resources/ih.wav"or wherever you are storing the file.

考虑到您相应地更改了文件的路径(例如或存储文件的任何位置),这应该会产生您正在寻找的结果"main/resources/ih.wav"

回答by nachokk

I'd create a resourcesfolder inside srcsomething like src/main/resources. You would like to take a look to Maventhat is project management and comprehension tool that help a lot in project organization.

我会resourcessrc类似 src/main/resources. 您想看看Maven,它是项目管理和理解工具,对项目组织有很大帮助。

回答by MadProgrammer

Caveat: I don't use Eclipse, I've never used Eclipse, my knowledge of Eclipse amounts to reading other posts and answering questions here

警告:我不使用 Eclipse,我从未使用过 Eclipse,我对 Eclipse 的了解相当于在这里阅读其他帖子和回答问题

Under your project, create a directory called resources. In here place all you "resources" you want to access from within your application. These will bundled with your application as embedded resources.

在您的项目下,创建一个名为resources. 在这里放置您想要从应用程序中访问的所有“资源”。这些将作为嵌入式资源与您的应用程序捆绑在一起。

From within your application use either

从您的应用程序中使用

this.getClass().getResource("/ah.wav")

or

或者

this.getClass().getResource("/resources/ah.wav")

Assuming that you placed the files at the top level of the resourcespath. If they're in a sub-directory, you'll need to provide the full path...

假设您将文件放在resources路径的顶层。如果它们位于子目录中,则需要提供完整路径...

Clean, build and test

清理、构建和测试