1

I'm a beginner in Java and I followed a tutorial to write this program (yes, I that much of a beginner) and it works perfectly when I run it on Eclipse. It also runs great on the computer I coded it on. However, if I send it to another computer (just the .jar file) and run it, it fails because it can't find the icon. Here is everything I've got. The icon I'm using is saved in the bin folder along with all the class files for the program. For privacy reasons, I replaced certain lines with "WORDS".

The tutorial I followed in two parts:

Part 1 - https://buckysroom.org/videos.php?cat=31&video=18027

Part 2 - https://buckysroom.org/videos.php?cat=31&video=18028

My main class (I called it apples cause the tutorial did).

import javax.swing.JFrame;

public class apples {
public static void main(String[] args) {
Gui go = new Gui();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(1920,1080);
go.setVisible(true);
}
}

And now my second class, "Gui":

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class Gui extends JFrame {

private JButton custom;

public Gui () {
    super("WORDS");
    setLayout(new FlowLayout());

    Icon b = new ImageIcon(getClass().getResource("b.png"));
    custom = new JButton(null, b);
    custom.setToolTipText("WORDS");
    add(custom);

    HandlerClass handler = new HandlerClass();
    custom.addActionListener(handler);
}

private class HandlerClass implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
        JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
        JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
        JOptionPane.showMessageDialog(null, String.format("WORDS", event.getActionCommand()));
    }
}
}

Thank you so much for helping!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sacha862
  • 11
  • 3
  • Hey Sacha862, Did you try adding the images in the same folder where you saved GUI and apples. try to save it in the src and check it out with your old code. – The Third Jul 17 '14 at 01:16
  • Hopefully this [post](http://stackoverflow.com/a/9866659/1057230), might be able to help you in this direction :-) – nIcE cOw Jul 17 '14 at 09:14

1 Answers1

2

It's worth reading Loading Images Using getResource where it's explained in detail along with loading images from jar as well.

You can try any one based on image location.

// Read from same package 
ImageIO.read(getClass().getResourceAsStream("b.png"));

// Read from src/images folder
ImageIO.read(getClass().getResource("/images/b.png"))

// Read from src/images folder
ImageIO.read(getClass().getResourceAsStream("/images/b.png"))

Read more...

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
  • Here `Icon b = new ImageIcon(getClass().getResource("b.png"));` insert it inside `new ImageIcon(...)` – Braj Jul 17 '14 at 00:59
  • I get the following error when running it in Eclipse: – Sacha862 Jul 17 '14 at 01:05
  • Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:181) at Gui.(Gui.java:18) at apples.main(apples.java:6) – Sacha862 Jul 17 '14 at 01:05
  • what is at line 6 of apples.java? Might be it's not loading image properly. – Braj Jul 17 '14 at 01:07
  • what is at line 18 in Gui.java. Follow the stacktrace. – Braj Jul 17 '14 at 01:08
  • Icon b = new ImageIcon(getClass().getResource("/images/b.png")); – Sacha862 Jul 17 '14 at 01:09
  • because i don't know what is at that line no. Try other option or read the tutorial for better understanding. – Braj Jul 17 '14 at 01:10
  • at line 18 of Gui.java, there is the following line of code: Icon b = new ImageIcon(getClass().getResource("/images/b.png")); – Sacha862 Jul 17 '14 at 01:11
  • Do you have `images` folder in your project? – Braj Jul 17 '14 at 01:12
  • the path is currently /Users/*user*/Desktop/*project*/images/b.png – Sacha862 Jul 17 '14 at 01:13
  • What I have mentioned in the post. It should be under the src folder of the project. click on read more.. – Braj Jul 17 '14 at 01:14
  • ok I put it there so the path is now /.../*project*/src/images/b.png – Sacha862 Jul 17 '14 at 01:16
  • yes now try or you can put the images in the same package(folder) directly where the class is present. – Braj Jul 17 '14 at 01:17
  • that worked. give me a minute to see if the jar file will execute correct;lt – Sacha862 Jul 17 '14 at 01:18
  • This came up: JAR creation failed. See details for additional information. Exported with compile warnings: ApprovedByLucie/src/Gui.java Resource is out of sync with the file system: '/ApprovedByLucie/src/.DS_Store'. – Sacha862 Jul 17 '14 at 01:20
  • Take your time and try to find out the actual issue. Why JAR creation failed? It's just warnings. – Braj Jul 17 '14 at 01:22