How do I add image files to a Java project in Eclipse JDT?

Discussion in 'Java' started by Warren Tang, Jun 6, 2009.

  1. Warren Tang

    Warren Tang Guest

    Hello,

    I want to load an image file like this:
    image = ImageIO.read(new File("pic.gif"));

    But I don't know where to put the gif file. It seems that I need to copy
    the gif file to where the executable resides, but where is the
    executable? Can I do it in Eclipse IDE or do I have to copy the file
    manually?

    My description might be confusing for I'm a beginner. If clarifications
    are needed please tell me. Thanks.

    Regards,
    Warren
    Warren Tang, Jun 6, 2009
    #1
    1. Advertising

  2. Warren Tang

    Mark Space Guest

    Warren Tang wrote:
    > Hello,
    >
    > I want to load an image file like this:
    > image = ImageIO.read(new File("pic.gif"));
    >
    > But I don't know where to put the gif file. It seems that I need to copy
    > the gif file to where the executable resides, but where is the
    > executable? Can I do it in Eclipse IDE or do I have to copy the file
    > manually?


    I don't know about Eclipse, but in NetBeans you add resources to the
    source files, not the executable ones. Then when the project is built
    all files are copied into the executable from the source files.

    Try drag and drop, copy and paste (control-c, control-v) into the source
    folder in your Project view and the File view. These might work.
    Mark Space, Jun 6, 2009
    #2
    1. Advertising

  3. In article <lIwWl.28184$>,
    Mark Space <> wrote:

    > Warren Tang wrote:
    > > Hello,
    > >
    > > I want to load an image file like this:
    > > image = ImageIO.read(new File("pic.gif"));
    > >
    > > But I don't know where to put the gif file. It seems that I need to copy
    > > the gif file to where the executable resides, but where is the
    > > executable? Can I do it in Eclipse IDE or do I have to copy the file
    > > manually?

    >
    > I don't know about Eclipse, but in NetBeans you add resources to the
    > source files, not the executable ones. Then when the project is built
    > all files are copied into the executable from the source files.


    If I may amplify, you can then read the the image from the JAR using
    getResource(), as suggested in "Loading Images Using getResource":

    <http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html>

    [...]
    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Jun 6, 2009
    #3
  4. Warren Tang

    Lew Guest

    John B. Matthews wrote:
    > If I may amplify, you can then read the the image from the JAR using
    > getResource(), as suggested in "Loading Images Using getResource":
    >
    > <http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html>


    The key to Class#getResource() and its sister methods (getResourceAsStream(),
    the corresponding methods for ClassLoader, and the Java EE methods by those
    names from ServletContext) is that they root in the classpath (or the context
    root in the case of the ServletContext methods). That is, if your class is
    com.lewscanon.foo.Bar and you want to load an image "flower.png", you can
    refer to it from Bar by absolute path "/com/lewscanon/foo/flower.png" or
    relative path "flower.png". The root is not the filesystem root but a
    classpath (or context) root, same as for the class itself.

    --
    Lew
    Lew, Jun 6, 2009
    #4
  5. On Jun 7, 4:59 am, "John B. Matthews" <> wrote:
    >..
    > ...you can then read the the image from the JAR using
    > getResource(), as suggested in "Loading Images Using getResource":
    >
    > <http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html>


    Ughh. Again!

    I had cause to issue some feedback to Sun re that
    very advice the other day..
    <http://forums.sun.com/thread.jspa?
    threadID=5389931&messageID=10730418#10730418>

    Use the context class loader, people. *Context
    class loader.*

    --
    Andrew T.
    pscode.org
    Andrew Thompson, Jun 7, 2009
    #5
  6. Warren Tang

    Warren Tang Guest

    Mark Space wrote:
    > Warren Tang wrote:
    >> Hello,
    >>
    >> I want to load an image file like this:
    >> image = ImageIO.read(new File("pic.gif"));
    >>
    >> But I don't know where to put the gif file. It seems that I need to
    >> copy the gif file to where the executable resides, but where is the
    >> executable? Can I do it in Eclipse IDE or do I have to copy the file
    >> manually?

    >
    > I don't know about Eclipse, but in NetBeans you add resources to the
    > source files, not the executable ones. Then when the project is built
    > all files are copied into the executable from the source files.
    >
    > Try drag and drop, copy and paste (control-c, control-v) into the source
    > folder in your Project view and the File view. These might work.


    Thanks for the suggestion but Copy & Paste won't do for Eclipse.
    Warren Tang, Jun 7, 2009
    #6
  7. Warren Tang

    Warren Tang Guest

    Andrew Thompson wrote:
    > On Jun 7, 4:59 am, "John B. Matthews" <> wrote:
    >> ..
    >> ...you can then read the the image from the JAR using
    >> getResource(), as suggested in "Loading Images Using getResource":
    >>
    >> <http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html>

    >
    > Ughh. Again!
    >
    > I had cause to issue some feedback to Sun re that
    > very advice the other day..
    > <http://forums.sun.com/thread.jspa?
    > threadID=5389931&messageID=10730418#10730418>
    >
    > Use the context class loader, people. *Context
    > class loader.*
    >
    > --
    > Andrew T.
    > pscode.org


    The page Join pointed to me really helps me understand (to some extent)
    a lot of concepts (package and the folder structure in particular).
    Whether it is a recommended way or not is another thing though.

    Based on my understandings, the Eclipse JDT sets the classpath to the
    project root folder. If I copied the image file to the root folder, the
    image file could be found without problem.
    Warren Tang, Jun 7, 2009
    #7
  8. Warren Tang

    Lew Guest

    Warren Tang wrote:
    > Based on my understandings, the Eclipse JDT sets the classpath to the
    > project root folder. If I copied the image file to the root folder, the
    > image file could be found without problem.


    Not exactly. Eclipse sets the classpath to your build folder. Let's say you
    have a typical project structure for project 'foo',

    ${projects}/foo/{src,test,build}

    then for a Java project 'build/classes/' is a root of the run-time classpath.
    The parallel source directory 'src/' is the root of the developer classpath.
    Your resource itself goes in the 'src/' subdirectory corresponding to some
    convention, e.g., 'images/', or to the invoking class. Thus:

    foo/src/com/lewscanon/foo/resource.png

    In this arrangement, class com.lewscanon.foo.Foo asks to
    getResource("resource.png")
    or
    getResource("/com/lewscanon/foo/resource.png")
    ..

    foo/src/images/resource.png

    In this arrangement, com.lewscanon.foo.Foo asks to
    getResource("/images/resource.png")
    ..

    For Java EE using ServletContext#getResource() resolution is against the
    context root rather than (actually, in addition to and before) the class path.
    Web resources go in the project's 'web/' somewhere, e.g.,
    'web/images/resource.png'. This deploys into
    '<context-root>/images/resource.png' - for Tomcat, something like

    ${TOMCAT_HOME}/webapps/foo/images/resource.png

    --
    Lew
    Lew, Jun 7, 2009
    #8
  9. Warren Tang

    Lew Guest

    Mark Space wrote:
    >> Try drag and drop, copy and paste (control-c, control-v) into the
    >> source folder in your Project view and the File view. These might work.


    Warren Tang wrote:
    > Thanks for the suggestion but Copy & Paste won't do for Eclipse.


    What won't copy-and-paste do for Eclipse? Copy files? I've used that feature
    and it copies files just fine.

    --
    Lew
    Lew, Jun 7, 2009
    #9
  10. Warren Tang

    Mark Space Guest

    Lew wrote:
    > Mark Space wrote:
    >>> Try drag and drop, copy and paste (control-c, control-v) into the
    >>> source folder in your Project view and the File view. These might work.

    >
    > Warren Tang wrote:
    >> Thanks for the suggestion but Copy & Paste won't do for Eclipse.

    >
    > What won't copy-and-paste do for Eclipse? Copy files? I've used that
    > feature and it copies files just fine.
    >



    The copy & paste I'm referring to is for copy files external to the IDE
    and then pasting them into it. This works fine for NetBeans and
    Explorer. I can copy files (Control-C) in Explorer and then dump them
    where I want in NetBeans file view. I suspect that it works for Eclipse
    too, the OP just doesn't have the right view up or something like that.
    Mark Space, Jun 7, 2009
    #10
  11. In article
    <>,
    Andrew Thompson <> wrote:

    > On Jun 7, 4:59 am, "John B. Matthews" <> wrote:
    > >
    > > ...you can then read the the image from the JAR using
    > > getResource(), as suggested in "Loading Images Using getResource":
    > >
    > > [Painful link elided.] :)

    >
    > Ughh. Again!
    >
    > I had cause to issue some feedback to Sun re that
    > very advice the other day..
    > <http://forums.sun.com/thread.jspa?

    threadID=5389931&messageID=10730418#10730418>
    >
    > Use the context class loader, people. *Context class loader.*


    I see that if the context ClassLoader is not set, "the default is the
    ClassLoader context of the parent Thread." In retrospect, I see most of
    my resources are loaded on the EDT. Would that explain why problems
    using the tutorial approach aren't more common?

    <http://java.sun.com/javase/6/docs/api/java/lang/Thread.html>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
    John B. Matthews, Jun 8, 2009
    #11
  12. Warren Tang

    Warren Tang Guest

    Mark Space wrote:
    > Lew wrote:
    >> Mark Space wrote:
    >>>> Try drag and drop, copy and paste (control-c, control-v) into the
    >>>> source folder in your Project view and the File view. These might
    >>>> work.

    >>
    >> Warren Tang wrote:
    >>> Thanks for the suggestion but Copy & Paste won't do for Eclipse.

    >>
    >> What won't copy-and-paste do for Eclipse? Copy files? I've used that
    >> feature and it copies files just fine.
    >>

    >
    >
    > The copy & paste I'm referring to is for copy files external to the IDE
    > and then pasting them into it. This works fine for NetBeans and
    > Explorer. I can copy files (Control-C) in Explorer and then dump them
    > where I want in NetBeans file view. I suspect that it works for Eclipse
    > too, the OP just doesn't have the right view up or something like that.


    I tried to paste an image file into the Package View as well as the
    Navigator view in Eclipse. Both views complained something like "cannot
    paste content in the clipboard to the selected element ...". I selected
    the project by the way.
    Warren Tang, Jun 8, 2009
    #12
  13. Warren Tang

    Warren Tang Guest

    Lew wrote:
    > Warren Tang wrote:
    >> Based on my understandings, the Eclipse JDT sets the classpath to the
    >> project root folder. If I copied the image file to the root folder,
    >> the image file could be found without problem.

    >
    > Not exactly. Eclipse sets the classpath to your build folder. Let's
    > say you have a typical project structure for project 'foo',
    >
    > ${projects}/foo/{src,test,build}
    >
    > then for a Java project 'build/classes/' is a root of the run-time
    > classpath. The parallel source directory 'src/' is the root of the
    > developer classpath. Your resource itself goes in the 'src/'
    > subdirectory corresponding to some convention, e.g., 'images/', or to
    > the invoking class. Thus:
    >
    > foo/src/com/lewscanon/foo/resource.png
    >
    > In this arrangement, class com.lewscanon.foo.Foo asks to
    > getResource("resource.png")
    > or
    > getResource("/com/lewscanon/foo/resource.png")
    > .
    >
    > foo/src/images/resource.png
    >
    > In this arrangement, com.lewscanon.foo.Foo asks to
    > getResource("/images/resource.png")
    > .
    >
    > For Java EE using ServletContext#getResource() resolution is against the
    > context root rather than (actually, in addition to and before) the class
    > path. Web resources go in the project's 'web/' somewhere, e.g.,
    > 'web/images/resource.png'. This deploys into
    > '<context-root>/images/resource.png' - for Tomcat, something like
    >
    > ${TOMCAT_HOME}/webapps/foo/images/resource.png
    >


    Thanks for your detailed explanation. I don't think I can fully
    understand what you've said, given the my limited knowledge set about
    Java. Now that I've got something that "works", I'll dig into this topic
    later and I'm sure what you've said will be great references to me.
    Warren Tang, Jun 8, 2009
    #13
  14. Warren Tang

    danP

    Joined:
    Aug 17, 2010
    Messages:
    1
    I'm working on a homework assignment that requires me to access a bunch of image files, and I'm looking for a more organized way to do it right now. But if you have only a few image files, the executable folder for Eclipse is workspace/project/bin. I know that folder works for sound files.
    danP, Aug 17, 2010
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eddy C
    Replies:
    4
    Views:
    588
    Eddy C
    Mar 22, 2006
  2. Gunter Hansen
    Replies:
    10
    Views:
    41,711
    Paul Hamaker
    Apr 3, 2006
  3. Replies:
    1
    Views:
    318
  4. gc
    Replies:
    0
    Views:
    2,860
  5. narke
    Replies:
    2
    Views:
    340
    narke
    Sep 15, 2010
Loading...

Share This Page