Skip to main content



      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 05:10
Eclipse UserFriend
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)

Quote:

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 = iresStream.read(tmp, 0, 4096)) > -1 )
byteStream.write(tmp, 0, bytesRead);

iresStream.close();
// 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 08:56] by Moderator

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


Current Time: Sun May 18 04:47:54 EDT 2025

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

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

Back to the top