Add image to . jar

Asked

Viewed 2,826 times

2

I have several programs that run perfectly on my machine, the .jar runs smoothly and I use them as well as I wish, no problem. However, when I run them on another machine, images do not appear. The program runs perfectly, everything as it should be, but the images just don’t appear.

Here’s one of the programs I’d like you to run, a calculator. I’ve changed the icon of JOpTionPane.showMessageDialog, that is in the menu, and the icon does not appear.

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class Calculadora extends JFrame {
    private static final long serialVersionUID = 1L;

private JButton n1;
private JButton n2;
private JButton n3;
private JButton n4;
private JButton n5;
private JButton n6;
private JButton n7;
private JButton n8;
private JButton n9;
private JButton n0;
private JButton botaoMais;
private JButton botaoMenos;
private JButton botaoVezes;
private JButton botaoDividi;
private JButton botaoIgual;
private JButton botaoC;
private JButton botaoCE;
private JButton botaoPonto;
private char operacao;
private int inteiro;
private int decimal;
private double memoria;
private boolean ponto;
private JLabel creditos;
private JTextField campo;

public Calculadora(){
    setTitle("Calculadora do Ozzy");
    setBounds(0,0,280,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    getContentPane().setBackground(new Color(190,194,233));
    setLayout(null);

    JMenuBar mBar = new JMenuBar();

    JMenu opcoes = new JMenu("Opções");
    JMenuItem sobreCalc = new JMenuItem("Sobre");
    JMenuItem sobreMim = new JMenuItem("Sobre mim");

    opcoes.add(sobreCalc);
    opcoes.add(sobreMim);

    JMenu sair = new JMenu("Sair");
    JMenuItem exit = new JMenuItem ("Sair");

    sair.add(exit);

    mBar.add(opcoes);
    mBar.add(sair);
    setJMenuBar(mBar);

    inteiro = 0;
    decimal = 0;
    memoria = 0;

    n1 = new JButton ();
    n1.setText("1");
    n1.setBounds(25,115,45,45);
    n1.setBackground(new Color(134,142,215));
    n1.setForeground(Color.white);
    this.add(n1);

    n2 = new JButton ();
    n2.setText("2");
    n2.setBounds(85,115,45,45);
    n2.setBackground(new Color(134,142,215));
    n2.setForeground(Color.white);
    this.add(n2);

    n3 = new JButton();
    n3.setText("3");
    n3.setBounds(140,115,45,45);
    n3.setBackground(new Color(134,142,215));
    n3.setForeground(Color.white);
    this.add(n3);

    n4 = new JButton();
    n4.setText ("4");
    n4.setBounds (25,170,45,45);
    n4.setBackground(new Color(134,142,215));
    n4.setForeground(Color.white);
    this.add(n4);

    n5 = new JButton ();
    n5.setText("5");
    n5.setBounds(85,170,45,45);
    n5.setBackground(new Color(134,142,215));
    n5.setForeground(Color.white);
    this.add (n5);

    n6 = new JButton();
    n6.setText("6");
    n6.setBounds(140,170,45,45);
    n6.setBackground(new Color(134,142,215));
    n6.setForeground(Color.white);
    this.add(n6);

    n7 = new JButton ();
    n7.setText("7");
    n7.setBounds(25,225,45,45);
    n7.setBackground(new Color(134,142,215));
    n7.setForeground(Color.white);
    this.add(n7);

    n8 = new JButton();
    n8.setText("8");
    n8.setBounds (85,225,45,45);
    n8.setBackground(new Color(134,142,215));
    n8.setForeground(Color.white);
    this.add (n8);

    n9 = new JButton ();
    n9.setText("9");
    n9.setBounds (140,225,45,45);
    n9.setBackground(new Color(134,142,215));
    n9.setForeground(Color.white);
    this.add (n9);

    n0 = new JButton();
    n0.setText ("0");
    n0.setBounds (140,280,45,45);
    n0.setBackground(new Color(134,142,215));
    n0.setForeground(Color.white);
    this.add (n0);

    botaoMais = new JButton();
    botaoMais.setText("+");
    botaoMais.setBounds(195,115,45,45);
    botaoMais.setBackground(new Color(133,197,235));
    botaoMais.setForeground(Color.white);
    this.add(botaoMais);

    botaoMenos = new JButton ();
    botaoMenos.setText ("-");
    botaoMenos.setBounds(195,170,45,45);
    botaoMenos.setBackground(new Color(133,197,235));
    botaoMenos.setForeground(Color.white);
    this.add (botaoMenos);

    botaoVezes = new JButton();
    botaoVezes.setText ("x");
    botaoVezes.setBounds(195,225,45,45);
    botaoVezes.setBackground(new Color(133,197,235));
    botaoVezes.setForeground(Color.white);
    this.add(botaoVezes);

    botaoDividi = new JButton();
    botaoDividi.setText("÷");
    botaoDividi.setBounds(195,280,45,45);
    botaoDividi.setBackground(new Color(133,197,235));
    botaoDividi.setForeground(Color.white);
    this.add(botaoDividi);

    botaoIgual = new JButton();
    botaoIgual.setText("=");
    botaoIgual.setBounds(25,280,45,45);
    botaoIgual.setBackground(new Color(133,197,235));
    botaoIgual.setForeground(Color.white);
    this.add(botaoIgual);

    botaoPonto = new JButton ();
    botaoPonto.setText(".");
    botaoPonto.setBounds(85,280,45,45);
    botaoPonto.setBackground(new Color(133,197,235));
    botaoPonto.setForeground(Color.white);
    this.add (botaoPonto);

    botaoC = new JButton ();
    botaoC.setText("C");
    botaoC.setBounds(25,65,105,40);
    botaoC.setBackground(new Color (157,182,210));
    botaoC.setForeground(Color.white);
    this.add(botaoC);

    botaoCE = new JButton ();
    botaoCE.setText("CE");
    botaoCE.setBounds(140,65,100,40);
    botaoCE.setBackground(new Color(157,182,210));
    botaoCE.setForeground(Color.white);
    this.add(botaoCE);

    campo = new JTextField();
    campo.setBounds(25,25,216,30);
    this.add(campo);

    creditos = new JLabel();
    creditos.setBounds(78,287,200,100);
    creditos.setText("Gabriel Ozzy Santos");
    this.add(creditos);

    n1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if(ponto){
                decimal +=1;
            } else{
            inteiro *= 10;
            inteiro +=1;

            }
            campo.setText(campo.getText()+ "1");
        }
    });
    n2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (ponto){
                decimal +=2;
            }else{
            inteiro *=10;
            inteiro+= 2;

            }
            campo.setText (campo.getText()+"2");
        }
    });
    n3.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            if (ponto){
                decimal+=3;
            }else {
            inteiro *= 10;
            inteiro += 3;
            }
            campo.setText(campo.getText()+"3");

        }
    });
    n4.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (ponto){
                decimal+=4;
            }else {
            inteiro *= 10;
            inteiro +=4;
            }
            campo.setText (campo.getText ()+"4");

        }
    });
    n5.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            if (ponto){
                decimal +=5;
            }else{
            inteiro*= 10;
            inteiro +=5;
            }
            campo.setText (campo.getText ()+ "5");
        }
    });
    n6.addActionListener (new ActionListener (){
        public void actionPerformed (ActionEvent evt){
            if (ponto){
                decimal +=6;
            }else {
            inteiro*= 10;
            inteiro +=6;
            }
            campo.setText (campo.getText ()+ "6");
        }
    });
    n7.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            if(ponto){
                decimal +=7;
            }else {
            inteiro*= 10;
            inteiro +=7;
            }
            campo.setText (campo.getText()+"7");
        }
    });
    n8.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if(ponto){
                decimal +=8;
            }else{
            inteiro *=10;
            inteiro +=8;
            }
            campo.setText(campo.getText()+"8");
        }
    });
    n9.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (ponto){
                decimal +=9;
            }else{
            inteiro *=10;
            inteiro +=9;
            }
            campo.setText(campo.getText()+"9");

        }
    });
    n0.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if(ponto){
                decimal +=0;
            }else{
            inteiro*=10;
            inteiro +=0;
            }
            campo.setText(campo.getText()+"0");
        }
    });
    botaoMais.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero =Double.parseDouble(String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '+';
            if (memoria > 0){
                memoria += numero;
            }else{
                    memoria = numero;
                }
            inteiro = 0;
            numero = 0;
            decimal = 0;
            ponto = false;
            campo.setText("");
            }
        });
    botaoMenos.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero = Double.parseDouble (String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '-';
            if (memoria > 0){
                memoria -= numero;
            }else {
                memoria = numero;
            }
            numero = 0;
            inteiro = 0;
            decimal = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoVezes.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero = Double.parseDouble (String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '*';
            if (memoria > 0){
                memoria *= numero;
            }else {
                memoria = numero;
            }
            numero = 0;
            inteiro =0;
            decimal =0;
            ponto = false;      
            campo.setText("");
        }
    });
    botaoDividi.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            double numero = Double.parseDouble(String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '/';
            if(memoria > 0){
                memoria /=numero;
            }else {
                memoria = numero;
            }
            numero=0;
            inteiro =0;
            decimal = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoC.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            inteiro = 0;
            decimal = 0;
            memoria = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoCE.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            inteiro = 0;
            decimal = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoPonto.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            if (!ponto){
                campo.setText(inteiro +".");
                ponto = (true);
            }
        }
    });
    botaoIgual.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero = Double.parseDouble(String.format("%s.%s", inteiro, decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            switch (operacao){
            case '+':{
                memoria += numero;
                break;
            }
            case '-':{
                memoria -=numero;
                break;
            }
            case '*':{
                memoria *=numero;
                break;
            }
            case '/':{
                memoria /=numero;
                break;
            }
            }
            numero = 0;
            campo.setText(""+ memoria);
        }
    });

    sobreCalc.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            Icon about = new ImageIcon("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");  
            JOptionPane.showMessageDialog(null,"Programado em Java 7 - Plataforma Eclipse", "Sobre o software",0,about);
        }
    });
    sobreMim.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            Icon about2 = new ImageIcon("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");
            JOptionPane.showMessageDialog(null,"Programador: Gabriel Ozzy Santos","Sobre o desenvolvedor",0,about2);
        }
    });
    exit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
                System.exit(0);
            }
        });
}

public static void main (String [] args){
    Calculadora cCalculadora = new Calculadora();
        cCalculadora.setVisible(true);
}
}

Here is the mistake:

3 answers

3


I always put all the images I’ll use inside a new package, so when I generate one. I don’t have to worry about them because they’ll be inside.

I created a new package called Assets, and inside it I put the images I need and in another package I created a class to perform the image load test. According to the following structure: inserir a descrição da imagem aqui

How was the class that loads the images and I made a test if the image was loaded successfully.

inserir a descrição da imagem aqui

You can use several ways to load the image, I used a simple as needed. And the console output indicating that the image was successfully loaded.

inserir a descrição da imagem aqui

  • Hello, thank you for the answer ! Could you explain to me more precisely what Mediatracker is for ? thank you !

  • Mediatracker is a utility class for tracking the status of media objects. I used an attribute of the class that returns an integer value, the meaning of which is whether a media object was successfully loaded and compared with the return of the getImageLoadStatus() method. This method asked me to look at the attributes related to the media loading of the Mediatracker class, so after looking I made the comparison. I recommend reading the class documentation at http://docs.oracle.com/javase/7/docs/api/java/awt/MediaTracker.html.

  • Thank you, it worked perfectly here, after two whole days crazy behind it ! Hahaha, thank you !

  • Nothing, I’m glad it worked.

2

Create a . zip file with all your images and add in your Build Path.

Right-click your project folder > Build Path > Configure Build Path > Java Build Path > Libraries > Add Jars

Example:

inserir a descrição da imagem aqui

To use the image in your code, for example do:

JLabel lblImg1 = new JLabel("");
JLabel lblImg2 = new JLabel("");
lblImg1.setIcon(new ImageIcon(Principal.class.getResource("/img1.png"))); 
lblImg2.setIcon(new ImageIcon(Principal.class.getResource("/img2.png"))); 

Use the name of the images that are inside your zip.

When exporting the jar it will automatically include your file . zip containing the images.

  • I did that and the Eclipse gave the "Compute Launch error" only solved after I have removed... some other way ?

  • @Gabrielozzy was a build error or was it an exception thrown during execution? could add stacktrace() or an error image?

  • The same instant I finished adding the . zip, this error appeared and Eclipse simply did not respond to the execution of any other program... The image is in question, which I just edited!

  • @Gabrielozzy did you ever restart Eclipse and try again? I’m not sure the error was caused by adding the file. zip build path. What types of images are you using? If it is jpeg according to your code it is not to give problem.

  • I restarted, but it didn’t work... the types of images are JPG even, I don’t understand the reason of the problem...

  • @Gabrielozzy the error persists? I have been researching about this error and seems to me to be an eclipse bug, however I do not know how it is caused or how to fix. Just explain me one thing, when you add the zip it goes on to give this error and when you remove it works again, is that it? I don’t know if it solves and I don’t think it’s good advice, but if you want to risk it, try updating your Eclipse to the latest version.

  • Unfortunately I already use the latest version... I will try to find another way, thanks friend !

Show 2 more comments

1

I don’t know if it’s been resolved, but I’ll answer it. When I use images in my projects I create a folder inside the project folder and do so:

Icon about = new ImageIcon("minhaPasta/Java2.jpg");

And when I use it on another machine I copy the jar and this folder. It always works.

  • good answer, just make it a little clearer how should the folder structure, like, your jar and image folder should be on the same level, correct? if you want to edit your reply and I delete my comment from there

  • It hasn’t been solved yet... but then, in that case, you’d always have to copy the . jar and the folder together, is that it? It’s just that it would be complicated... there must be another way, I believe...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.