This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Passing ImageIcon with RMI  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Thanks in advance!

here is some of the code i used

--in client RMI interface---


--in server--


i have also tried:


but still does not work

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
try this
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. ]
 
Jason gow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that would be in the server.jar?

the problem isnt with the server(or so i think) because it get the image correctly(i have tested it), i think the problem is when it gets passed

it is strange to me because it works perfectly fine in netbeans and then doesnt when i created a .jar file

I have tested this RMI server/client that i have set up and only made the client side a .jar, running the server in netbeans and thats where i sit with the problem

Thanks for your help so far!

[UD: Edited for brevity. There is no point in quoting an entire post, much less if the reply comes right after it. ]
 
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:RMI can only properly transfer primitives and objects that are serializable. ImageIcon is not serializable.


who says imageicon is not Serializable ? refer this http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/ImageIcon.html
 
Jason gow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Rob Spoor
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naved momin wrote:

Rob Spoor wrote:RMI can only properly transfer primitives and objects that are serializable. ImageIcon is not serializable.


who says imageicon is not Serializable ? refer this http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/ImageIcon.html


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.
 
Jason gow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for late reply guys.

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
 
Jason gow
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no knows what this could be??
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!