简体   繁体   中英

Trouble exporting to runnable jar in java eclipse project with images

I am having trouble with exporting my first project to an executable jar file. It runs just fine in eclipse but when I click double click on the executable jar after I export it, it does nothing. I have created multiple test programs and searched the internet for help, and have concluded that it has something to do with the images in the program. I have included all my code from the test program. It won't let me post the picture of the location of the image file in my project but it is in a source folder.

import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class test {

    private JFrame frame;
    JLabel label;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test window = new test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public test() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        label = new JLabel("");
        Image img = new   ImageIcon(this.getClass().getResource("/YahtzeeBoard.png")).getImage();
        label.setIcon(new ImageIcon(img));
        label.setBounds(100, 100, 450, 300);
        frame.getContentPane().add(label);
    }

}

Make a java package like Icons for example , place the image in there , and then call

 getResource("Icons/YahtzeeBoard.png")

this way the images will be packaged inside the jar .

or you can put the images in the same folder as the jar for the path "/YahtzeeBoard.png" to be found , since that points to the folder where the jar is .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM