How to Add a Resources Folder to My Java Project in Eclipse

Putting resources into my eclipse project

of course there is no point in putting files in the bin as it may be rebuilt by Eclipse whenever needed and you'll loose your changes.

by default Eclipse is set up to rebuild the project automatically, you can see that by looking in the "Project" menu entry, you can force Eclipse to rebuild your project by choosing "clean" in that "Project" menu.

So now you should have more control over when Eclipse is building your project.

Regarding adding resources files, I'm not sure what your current project directory structure is, in order to add / remove directories in the project, right click on the project root in the project Explorer side pane and choose "Properties" entry (at the bottom), then you can go to "Java Build Path" -> "Source" tab and manage the project directories.

last, if you change files on your disk and not through Eclipse, right click on the project root and choose "refresh" entry.

Hope this helps.

How to create a src/main/resources directory?

To add a resource folder in eclipse:

  • Click on Build Path

  • Click on Configure Build Path

    (or Properties -> Java Build Path)

  • Click on Source Tab

    Sample Image

    Click on Add Folder

    Sample Image

  • Click on Create new Folder

    How to include the resources folder in a jar file

    Follow these steps:

    1. click project -> properties -> Build Path -> Source -> Add Folder and select resources folder.

    2. create your JAR!

    EDIT: you can make sure your JAR contains folder by inspecting it using 7zip.

    Reefer this link as well How do I add a resources folder to my Java project in Eclipse

    How do I add a directory to the eclipse classpath?

    In Eclipse, there is a build classpath and a runtime classpath. There is also the build output location, which by default is bin. You don't want to add resources directly to bin because Eclipse can delete its contents when doing a clean build. What you need to do is add a resources folder in your project to contain any non-Java files that you want included in your build output.

    To include the contents of this resources folder in the build output (bin), right-click the project and select Properties. In the Project Properties, select the Java Build Path section, then the Source tab.

    Sample Image

    Use the Add Folder... button to select the resources folder from your project, then OK to save the changes. At that point, Eclipse will automatically copy everything from resources into bin when it builds.

    Where should I put my resources for a Java program?

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

    Depending on preference, either put this folder in your main project folder MyProject/res or put it inside the relevant package, e.g. 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:

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

    That should produce 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.



    Related Topics



  • Leave a reply



    Submit