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

前端 未结 7 1705
梦谈多话
梦谈多话 2020-12-17 01:39

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 everythi

相关标签:
7条回答
  • 2020-12-17 02:11

    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.

    0 讨论(0)
  • 2020-12-17 02:12

    I had the same problem So I just past image in both folders 'bin' and 'src' in my case I have this code:

    Image image2 = new Image(getClass().getResourceAsStream("IMAGES/Main-icon.png"));
    

    run App and its will work

    0 讨论(0)
  • 2020-12-17 02:18

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

    0 讨论(0)
  • 2020-12-17 02:18

    See if you project has a "res" folder, which stands for resources. If it does, in that folder there should be a "Img" folder. You should put them there.

    0 讨论(0)
  • 2020-12-17 02:26

    I would convert project to Maven and do maven install.

    0 讨论(0)
  • 2020-12-17 02:28

    for Eclipse you may just create folder directly in src folder (or in some its package). Then Copy and Paste your image files into created folder. If You need to get proper path till current image just use Class.getResource("yourCreatedFolder/yourImage.someExtension")

    0 讨论(0)
提交回复
热议问题