Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse's workspace: Shall I put my images in 'src' or 'bin' folder?

I'm working in a project in Java and sometimes all my images randomly dissapeared from the project's bin folder. It is getting very annoying because I have to put everything again every time it happens. Someone told me that I shouldn't put my extra files in bin but in src. But eclipse doesn't read my images if I put them in src, as if they weren't there. Why is this happening? Thanks.

like image 595
Rama Avatar asked Aug 27 '11 20:08

Rama


People also ask

What should I put in my src folder?

The /src folder comprises of the raw non-minified code. The /src folder is used to store the file with the primary purpose of reading (and/or editing) the code. The /src folder contains all the sources, i.e. the code which is required to be manipulated before it can be used.

Where do I put images in Eclipse Dynamic Web project?

You have to add the image within the eclipse project structure for eclipse to package it and have it available on the webserver. Just having the image on your local file system doesn't work. To do this, drag and drop the file from your file system onto the eclipse project tree folder.

What is bin src?

src is where source code goes. bin is where binaries go, and which are usefully in a users' search PATH for places to look for executables. Usually the build process involves taking the files in source and turning them into files in bin.

What is bin folder in Eclipse project?

The bin folder can be ignored. This is nothing more than the folder in which Eclipse stores the . class files generated by compiling your code (which it does automatically; see compiling).


3 Answers

Create one resources folder and put them there. Then mark that folder as "source folder" (right click -> Build Path -> Use as source folder)

like image 140
Bozho Avatar answered Oct 22 '22 19:10

Bozho


Follow these two steps. Worked well for me in Eclipse Kepler.

  1. Bring your image or other resource files into your Java project

    i. Mouse right click on your Java project and do: New -> Source Folder. In this example, I call my folder "res".

    ii. Go to your file browser and you should see this "res" folder under your Java project's root folder. Now copy or move your image and other resource files into it.

    iii. Now go to Eclipse and right click on this "res" folder and do: Refresh. You should see these files show up in it.

    iv. Now build the project and you should see your resource files get copied into the build target "bin" folder.

  2. Now in your class (e.g. your own JFrame class as indicated by the this in my sample code below) you can access the resource file and note the leading "/" to indicate file location relative to binary root:

    Image img = new ImageIcon(this.getClass().getResource("/MyImage.gif")).getImage();
    

Another note: you can use multiple such resource folders to organize your resources. After project build, they all go into the build target root "bin" folder. And of course, you can export your project as "Runnable Jar File", then you can run your application standalone since all resources are automatically packaged into your jar file.

like image 44
ethan.zz Avatar answered Oct 22 '22 19:10

ethan.zz


You shouldn't put them in the bin directory - that should be seen as an output-only directory, which could be wiped at any time.

If you use Class.getResource() or ClassLoader.getResource() (or similar APIs) then I'd expect it to work just fine from the src directory or anything else on your build path, assuming the default settings. You should check the settings though.

From the docs on the build path:

Resources existing in source folders are copied to the output folder unless the setting in the Java > Compiler > Building preference page specifies that the resource is filtered. The output folder is defined per project except if a source folder specifies its own output folder.

So you need to check the "Building" preference page to see if you're filtering out resources. If you look in the bin directory after building, you should be able to see them.

Note that you may well want to put the resources in their own directory just for organizational purposes - that's fine, and you can do it either within the src directory or by creating another directory on the build path.

like image 6
Jon Skeet Avatar answered Oct 22 '22 19:10

Jon Skeet