LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-30-2009, 12:04 PM   #1
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Java: Referencing an image within a package


Hello everyone. I am running into a small bit of difficulty with images I want to use in a Java program, keeping them bundled with the program in a JAR file and accessing them within the package.

For those who are thinking it, this is definitely not homework.

In my program, I have several images I would like to use on buttons on a toolbar. I am under the impression
Code:
JButton button = new JButton(new ImageIcon("icon.png"));
is referencing the filesystem (the directory location where the JAR exists) but what I want is to be able to access the resource within the JAR. Further explanation: if the JAR I am running is located at /home/indienick/program.jar, the ImageIcon class is (probably) looking for the image at /home/indienick/icon.png.

I have Googled, and I found something that comes close, but it deals with fetching images from a URL - nothing that deals with local files, more specifically, local images kept inside of a JARchive.

Any help would be greatly appreciated.

Last edited by indienick; 01-30-2009 at 12:05 PM.
 
Old 01-30-2009, 01:07 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

I think the word "URL" might be misleading here. A URL *can* be a local file (as well as a remote web link), and I think it's probably exactly what you want:

http://forums.sun.com/thread.jspa?threadID=5347567

Code:
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
 
public class DisplayJarPNG
{
    public static void main(String[] args) throws MalformedURLException, 
        IOException
    {
        URL url = new 
            URL("jar:file:/C:/lib/images.jar!/sameDir/sameImage.png");
        Image image = ImageIO.read(url);
 
        // Use a label to display the image in a frame
        JFrame frame = new JFrame();
        JLabel label = new JLabel(new ImageIcon(image));
        frame.getContentPane().add(label, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
        frame. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
'Hope that helps .. PSM
 
Old 02-01-2009, 07:15 PM   #3
mrcheeks
Senior Member
 
Registered: Mar 2004
Location: far enough
Distribution: OS X 10.6.7
Posts: 1,690

Rep: Reputation: 52
use
Code:
new ImageIcon(getClass().getResource("myimage.png"));
 
Old 02-01-2009, 10:02 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
mrcheeks -

You're correct, but I wanted to reassure indienick that the "read java.net.URL" approach was also correct (and, in fact, the one Sun shows in the on-line Java docs).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Package Searching: Which Iso Image? stf92 Linux - Newbie 1 04-13-2008 03:30 AM
Image Processing Package win2suse Linux - Software 1 05-09-2007 08:37 AM
Java Question - referencing variables by name as a string qscomputing Programming 3 01-31-2007 04:49 AM
Synaptic Package Manager shows 'java-common' and 'java-gcj-compat' as Installed. swiadek Ubuntu 3 02-12-2006 11:54 AM
Java can't find package, but PATH set correctly (j2se1.4.0, java.util.regex package) Ethan Programming 5 02-06-2004 09:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration