I've been sitting on this for some time now where i pass a ImageIcon from the server to the client when the client asks for it. It works perfectly in netbeans but as soon as i create a jar file of it the IconImage wont load!
please help me as i have been sitting on this for ever
this is because ImageIcon doesn't get the image which you are trying to give him
First where is your image is located ?
is it located in the src directory ?
create a new directory called Images and keep your images in that .... Images directory should be next to dist directory in which you have placed your *.jar (in netbeans it is dist) directory and use this method to create a absolute path , modify this method so that it returns the correct path where your image is located then use (while running from *.jar it returns the path where *.jar is located in the file system)
and then use this to read resource(images)
Note: Testing is a class name for demonstration purpose
[UD: Edited for brevity. There is no point in quoting an entire post, much less if the reply comes right after it. ]
RMI can only properly transfer primitives and objects that are serializable. ImageIcon is not serializable. That means that you would need to find a way to serialize the ImageIcon.
One way would be to get the ImageIcon's Image, and use ImageIO and a ByteArrayOutputStream to turn it into a byte, transfer that, and use ImageIO and a ByteArrayInputStream to turn the byte back into an Image which you can use to construct a new ImageIcon. However, the saving and loading may modify the image. Better would be to turn the resource into a byte*, transfer that, and simply use the ImageIcon constructor that takes a byte.
* Use getResourceAsStream, and copy its contents to a ByteArrayOutputStream.
Jason gow wrote:Ok so how would i do it then?
Ive been trying so much ways but unless im doing it wrong
i have no idea how to do it
Please if you could explain it would be most helpful!
Thank you in advance
First what i understand from your code is that your image "image.jpg" is in the same directory as your src code (.java), and you have not shown us how you are reading the image from inside the *.jar
second buggy programs can work properly inside netbeans but fails to work when you run it from *.jar (my personal experience)
Third if you are able to successfully read Image from within the *.jar then Serialize ImageIcon object and send it using output stream of your Socket .... to the client and in the Client side cast it to appropriate ImageIcon object and display it in label or do whatever you want as simple is that
I stand corrected. ImageIcon apparently does its own image-to-byte conversion and vice-versa.
Jason, you said it works fine when you were running the application from Netbeans. Is the image file properly packaged inside your JAR file? You should then use getClass().getResource(xxx) to get a URL to the image, and pass that to the ImageIcon constructor.
My first thought was also that im not getting the image correctly but i dont believe that is the case.
My server get the image fine and im not running it within a .jar file(im running it from with in netbeans)
My client only wont work when i create a .jar file with it(works fine in netbeans) but i dont understand this as the client's .jar file does not hold the image at all.
it only get the image from the server when a method is call, so the image comes directly from the server