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

W

Warren Tang

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
 
M

Mark Space

Warren said:
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.
 
L

Lew

John said:
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.
 
W

Warren Tang

Mark said:
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.
 
W

Warren Tang

Andrew said:
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.*

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.
 
L

Lew

Warren said:
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
 
L

Lew

Warren said:
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.
 
M

Mark Space

Lew said:
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.
 
J

John B. Matthews

Andrew Thompson said:
...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>
 
W

Warren Tang

Mark said:
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.
 
W

Warren Tang

Lew said:
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.
 
Joined
Aug 17, 2010
Messages
1
Reaction score
0
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top