Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » General question - import image and write into jar file?
General question - import image and write into jar file? [message #786354] Mon, 30 January 2012 10:10
Ben S. is currently offline Ben S.Friend
Messages: 6
Registered: January 2012
Junior Member
Hi guys.
I spend almost an hour searching on google but I didnt find what I was searching for.
I've written a program which uses some images embedded in the frames.
These images have to be in a specific folder, otherwise the program is not able to read and show them.
If I want to send the program to friends, which may not know how to copy the files to the right path, the program is not working at all.

So here comes the point. Is it somehow possible to scan and write images into the bytecode / the jar file? (its a couple of small images, some kb) I mean, could I compile the image data so that the program got the necessary images within itself? Or am I raping the entire java programing concept with this question?

Thank you!

Edit: found solution (if somebody cares)


private byte[] resourceToByteArray(String resName) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
InputStream iresStream = getClass().getResourceAsStream(resName);
int bytesRead = 0;
byte[] tmp = new byte[4096];

while ( (bytesRead =, 0, 4096)) > -1 )
byteStream.write(tmp, 0, bytesRead);

// JDocs: "Closing a ByteArrayOutputStream has no effect."

return byteStream.toByteArray();


here comes the real code


try {
icon = new ImageIcon(resourceToByteArray("u12.jpg"));
icon2 = new ImageIcon(resourceToByteArray("zw1217.jpg"));
icon3 = new ImageIcon(resourceToByteArray("17.jpg"));
icon4 = new ImageIcon(resourceToByteArray("ue18.jpg"));
catch (IOException ex) {
// Fehlerbehandlung nach Wunsch...


[Updated on: Mon, 30 January 2012 13:56]

Report message to a moderator

Previous Topic:How to add a maven repository
Next Topic:Help using the gcj-ecj
Goto Forum:

Current Time: Mon Jun 25 16:06:45 GMT 2018

Powered by FUDForum. Page generated in 0.01305 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top