Results 1 to 4 of 4
  1. #1
    amman is offline newbie
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Jar file making - picture problem

    Hi everyone!

    I have a big problem. Me and my team made a java project (for our studies), and i need to make a jar of it.

    I tell you what i've tried (and then how i've fall):
    I have a package named "graph", so i have a folder named "graph".
    In this folder i have several source files (*.java), and 2 other folder named "model" and "images". The folder "model" contains more sourcefile, and the folder "images" contains some *.jpg picture, what i need for my program.

    I made a build.bat and a run.bat file with these containments:
    build.bat:
    Java Code:
    javac graph/*.java graph/model/*.java
    jar cvfe app.jar graph/Control graph/*.class graph/model/*.class graph/images/*.jpg
    run.bat:
    Java Code:
    java -jar app.jar
    The program is running, but i have a big problem: i can't see the pictures...
    In one of the files whitch uses a picture i use it like this:
    Java Code:
    icon = new ImageIcon("images/switchfalse.jpg");
    What could be the problem?

    Please help!
    Thanks,
    Amman

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    The ImageIcon constructor you use requires a String that represents a file name. A resource inside a jar isn't a file.

    Learn here how to construct an ImageIcon from an image in a jar, using a URL obtained from getResource(...)
    How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)


    db
    Last edited by DarrylBurke; 05-06-2011 at 06:50 AM. Reason: Typos...

  3. #3
    amman is offline newbie
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Hi!

    I tried to figure out how to do this magic with the resources, but i couldn't.
    Can you help me?

    Here is the code, where i'm using the picture:
    Java Code:
    package graph;
    
    import graph.model.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class SwitchView extends JLabel{
    
        private Switch sw;
        ImageIcon icon;
    
        SwitchView(String viewname, Switch newsw)
        {
            super(viewname);
            
            sw = newsw;
            //java.net.URL imageURL = java.lang.ClassLoader.class.getResource("images/switchfalse.jpg");
            icon = new ImageIcon("switchfalse.jpg");
            //icon = new ImageIcon(imageURL);
            super.setIcon(icon);
            this.addMouseListener(new MouseAdapter(){
                public void mouseClicked(MouseEvent e) {
                         SwitchView swview = (SwitchView)e.getSource();
                         
                         if(swview.sw.getValue())
                         {
                        	 swview.setIcon(new ImageIcon("switchfalse.jpg"));
                        	 swview.sw.switchSwitch();
                         }
                         else
                         {
                        	 swview.setIcon(new ImageIcon("switchtrue.jpg"));
                        	 swview.sw.switchSwitch();
                         }
                }
            });
        }
    
    }
    This works, if i run in under eclipse.
    But now i tried to replace the
    Java Code:
    icon = new ImageIcon("switchfalse.jpg");
    line with
    Java Code:
    //java.net.URL imageURL = java.lang.ClassLoader.class.getResource("images/switchfalse.jpg");
            icon = new ImageIcon(imageURL);
    but i'm getting nullpointerexception for the line "icon = new ImageIcon(imageURL);"

    To refresh the info about the folder hierarchy in the jar:

    /graph <-sourcefiles
    /graph/model <-sourcefiles
    /graph/images <-images

  4. #4
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    You need to include the pictures in the same path as you coded them to be. So in this case, your jar file must contain pictures in the same directory as your class file.

    Java Code:
    jar cvfm program.jar manifest.mf *.class *.jpg

Similar Threads

  1. Picture not showing when create JAR file
    By kmckinley820 in forum New To Java
    Replies: 4
    Last Post: 03-19-2011, 10:11 PM
  2. Making a picture Warhol Style
    By chick in forum New To Java
    Replies: 4
    Last Post: 04-13-2010, 09:08 PM
  3. making a vCard file
    By Hussain Ali in forum Advanced Java
    Replies: 2
    Last Post: 02-24-2010, 04:22 AM
  4. Picture in a JFrame problem
    By saytri in forum New To Java
    Replies: 3
    Last Post: 01-12-2008, 09:44 AM
  5. Print a picture file
    By oli001 in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 01:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •