Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: In jar execution I can't see My picture[netbeans,ubuntu]

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default In jar execution I can't see My picture[netbeans,ubuntu]

    Hello
    I create a program in NetBeans when I run it It works properly and shows my photo but in when I execute jar file (final app) I can't see my photo

    The whole project is in zip file with photo
    Thanks
    import java.awt.*;
    import javax.swing.*;
     
    public class MyPanel extends JPanel {
        private static final long serialVersionUID = 1L;
     
     
        public void paintComponent(Graphics g){
     
            g.fillRect(0, 0, 200, 300);
            int red = (int)(Math.random()*255);
            int green = (int)(Math.random()*255);
            int blue =(int)(Math.random()*255);
            Color color =new Color(red, green, blue);
            g.setColor(color);
            g.fillOval(70, 70, 100, 100);
            Image image = new ImageIcon("netbeans.png").getImage();
            g.drawImage(image, 10, 20, this);
     
        }
     
     
    }
    ===================================
     
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
     
    public class Color*Button implements ActionListener{
     
        JFrame frame = new JFrame("Color Button Demo");
        JButton button = new JButton("Change circle color behind java logo");
        MyPanel panel = new MyPanel();
     
     
     
        public  void go(){
            frame.getContentPane().add(BorderLayout.SOUTH,button);
            frame.getContentPane().add(BorderLayout.CENTER,panel);
            button.addActionListener(this);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(300, 400);
            frame.setVisible(true);
     
     
     
     
        }
     
        public void actionPerformed(ActionEvent ae){
            frame.repaint();
        }
     
     
    }
    ==================================================
     
    public class ColorButtonApp {
     
     
        public static void main(String[] args) {
     
            ColorButton colorButton = new ColorButton();
            colorButton.go();
     
        }
     
    }
    Attached Files Attached Files


  2. #2
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: In jar execution I can't see My picture[netbeans,ubuntu]

    Quote Originally Posted by mohsen.noor View Post
            Image image = new ImageIcon("netbeans.png").getImage();
    The main question is that "netbeans.png" is a specification on the file-system and, more important, it's a relative specification .... relative to the "current" directory.
    What is your current directory when you launch your application-in-jar? I can't know .... it depends. Depends on how/from where you start the java launcher (java.exe o whatever is in other O.S.).

    To be clear: your jar is for example in C:\MyProjects\myapp.jar

    From command prompt you can do:
    C:\MyProjects>java -jar myapp.jar
    or
    C:\>java -jar MyProjects\myapp.jar

    Both commands (in blue) correctly start your jar .... but the "current" directory is different ("C:\MyProjects" the first, "C:\" the second time).

    If your image "belongs" to your application, you should load it as a "resource" using getResource/getResourceAsStream of java.lang.Class.
    If "netbeans.png" is in the same package of MyPanel, you can do:

    ImageIcon ii = new ImageIcon(MyPanel.class.getResource("netbeans.png"));

    And it works even if netbeans.png is into the jar.


    Final note: don't load images (any resource in general) in a paint/paintComponent method.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  3. The Following User Says Thank You to andbin For This Useful Post:

    mohsen.noor (January 5th, 2014)

Similar Threads

  1. JAR Run Perfect in My Ubuntu 12.04 but not in Windows or Another OS
    By Malsasa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2013, 10:06 PM
  2. Execution of .jar file: Could not find main class....Error'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 14th, 2012, 02:04 PM
  3. NetBeans vs .JAR
    By Gavin Fraser in forum Java Theory & Questions
    Replies: 4
    Last Post: August 30th, 2012, 04:49 PM
  4. HELP - placing picture on top of another picture in Java - JLayerPane
    By smasm in forum What's Wrong With My Code?
    Replies: 39
    Last Post: April 27th, 2012, 07:16 PM
  5. [SOLVED] Picture won't go with the .jar (Wrong code or compile error?)
    By Fermen in forum Object Oriented Programming
    Replies: 3
    Last Post: March 15th, 2011, 05:22 PM

Tags for this Thread