Image loading problem in Jar

View previous topic View next topic Go down

Java Image loading problem in Jar

Post by BIT0112-Rokon on Wed Dec 01, 2010 11:18 am

I think you guys are very much familiar with image loading problem in jar. Your problem is something like when a .jar of an application is created, the images in the application no longer appear.

Lets check a code -

Code:

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;

public class ImageLoading extends JFrame {
/** The location of image. */
private String imageLocation = "/home/rokon/Desktop/rokon.jpg";
private JButton button;
private ImageIcon imageIcon;

public ImageLoading() {
button = new JButton();
imageIcon = new ImageIcon(imageLocation);
button.setIcon(imageIcon);
add(button);
}
public static void main(String[] args) {
ImageLoading il = new ImageLoading();
il.setSize(300, 300);
il.setVisible(true);
}
}

I think, you guys tries this way. Here it will work properly. But when you will try to make it executable jar using eclipse tools and try to run it, you will find no image in your application jar.

but here I have a solution. Just you've to add an extra line.

Code:

URLClassLoader urlLoader = (URLClassLoader) this.getClass()
.getClassLoader();

now paste your image in a package and change your image location that means- if you paste your image in the package named 'mypack' then,

private String imageLocation = "mypack/rokon.jpg";

and change the line

Code:

imageIcon = new ImageIcon(imageLocation);

to

Code:

imageIcon = new ImageIcon(urlLoader.findResource(imageLocation));

Now make your jar, it will work properly.

cheers!


Last edited by BIT0122-Amit on Mon Dec 06, 2010 12:26 am; edited 1 time in total (Reason for editing : use appropriate icons for topics please.)

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Image loading problem in Jar

Post by BIT0220-Iftekhar on Mon Jan 03, 2011 7:17 pm

I informed you last night that it wasn't working for me. I tried it as the same you mentioned but for some weird reason, it didn't work. Surprised

I again tried this evening and it worked finally, I've got the images on my chessboard (my OOP1 project for 2nd sem.) after executing the jar. Smile
So, rep++ rokon bhai.

But I've got a new problem came along with it. Neutral

The images got stuck. That means, when I make a move in the chessboard, they are not repainted. This didn't happen before you asked to add the two new lines.

So, I can't play on it and thus can't upload the whole project in forum. Please get me rid of this problem.
I've attached the source code and the exe jar file for your convenience.

Sourse code is Here. Download size : 7.7KB
Exe jar file is Here. Download size : 18.85KB
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Image loading problem in Jar

Post by BIT0112-Rokon on Mon Jan 03, 2011 10:31 pm

I dint find any problem .. btw ..
I just look forward your source code. You didn't follow Java standard, code convention. This is very bad..
Variable name should be noun and should be start with lowercase and
capitalize first letter of each subsequent word that appears. you should not use underscore (_). Not clicked_color, it should clickedColor . Be care full about it. If you dont follow this standard, I have to pay more extra time to read your source code, that is not possible always, and I may get board reading source code. And its true for everybody.

Have look on java coding conversion.
http://iitdu.forumsmotion.com/t828-java-coding-convention#3017

Btw here is your source; I just modified image source and package.
and here is your jar. May be it should work as your source design but somehow I couldn't play it. Better write a user manual for it and it is a good practice and good for software users also ..


_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Image loading problem in Jar

Post by BIT0220-Iftekhar on Tue Jan 04, 2011 12:47 am

BIT0112-Rokon wrote:

Btw here is your source; I just modified image source and package.
and here is your jar. May be it should work as your source design but somehow I couldn't play it. Better write a user manual for it and it is a good practice and good for software users also ..

And that's the problem,I can't play it either. Because the images(pieces) are not moving after I've included those 2 lines of code in my source.

I haven't completed my whole project yet. Didn't make an user manual thereby.
And my source code was edited hundred times and looks such disorganized so. Very Happy
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Image loading problem in Jar

Post by BIT0102-Mohaimin on Tue Jan 04, 2011 2:47 am

And that's the problem,I can't play it either. Because the images(pieces) are not moving...
I could not download your source (Mediafire is too slow), but I may be I can help because we faced similar problems with our 3rd semester project.
But I need to know something first,
Do the pieces move to correct position after you resize the window?
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Image loading problem in Jar

Post by BIT0220-Iftekhar on Tue Jan 04, 2011 2:58 am

BIT0102-Mohaimin wrote:
Do the pieces move to correct position after you resize the window?

Didn't get about resizing window. Did you mean to 'maximize' or to 'change' the JFrame size manually in the source code ?
Anyway, in both occasions, the pieces didn't move Crying or Very sad
avatar
BIT0220-Iftekhar
Administrator-RC

Course(s) :
  • BIT

Blood Group : B+
Posts : 434
Points : 630

View user profile http://iftekharunplugged.blogspot.com

Back to top Go down

Java Re: Image loading problem in Jar

Post by BIT0102-Mohaimin on Tue Jan 04, 2011 3:24 pm

Didn't get about resizing window. Did you mean to 'maximize' or to 'change' the JFrame size manually in the source code ?
Either.

Anyway, in both occasions, the pieces didn't move
Then I cannot help without checking your code. But I dont think its because of Using class loader. Not sure though.
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Image loading problem in Jar

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum