Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:24:08+00:00 2026-06-16T01:24:08+00:00

In Eclipse, when I run the code, this works: import javax.swing.ImageIcon; import javax.swing.JFrame; import

  • 0

In Eclipse, when I run the code, this works:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


   public class Main {

    public static void main(String[] args) {

        JFrame frame = new JFrame("test viewing images");

        frame.setSize(600,300);     
        frame.setLocationRelativeTo(null); // centered on monitor   
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        /**
         * Menu Bar stuff
         */

        JMenuBar menuBar;
        JMenu menu;
        JMenuItem menuItem;

        // MENU BAR 
        menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        menuBar.setVisible(true);

        // MENU 1
        menu = new JMenu("File");
        menuBar.add(menu);

            // MENU 1 ITEM
            ImageIcon icon = new ImageIcon("src/Action-exit-icon.png");     
            menuItem = new JMenuItem("Exit Program", icon);
            menu.add(menuItem);


        frame.setVisible(true);

    }

   }

And here’s the File Structure from my Package Explorer:

ShowImage (project)
 > src / Main.java
 > src / Action-exit-icon.png

Also, this workspace is located in Z:\eclipse_projects

I can see the ImageIcon icon = new ImageIcon(“src/Action-exit-icon.png”); is working nicely, and the menuBar does it’s job.

Now let’s Export this project, and I’ll email the JAR to a friend of mine.

  1. Right-click project > Select Export
  2. Select Java > Runnable JAR File
  3. I choose the Main File in Launch configuration
  4. Export destination: my desktop
  5. Library handling: Extract required libraries into generated JAR
  6. go to my desktop, double-click the ShowImage.jar

The JFrame shows up, but the Action-exit-icon.png isn’t appearing at all.

When I open the ShowImage.jar, to view it’s contents, I see the Main.class, Action-exit-icon.png, META-INF.

Ok, I’m seriously confused about how to reference an image, or any resource now.
What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:24:09+00:00Added an answer on June 16, 2026 at 1:24 am
    new ImageIcon("src/Action-exit-icon.png"); 
    

    The String constructor for an ImageIcon presumes the string to represent a File path.

    This image is obviously an application resource, and will become an embedded resource by the time of deployment (in a Jar). Therefore it must be accessed by URL from the run-time class-path of the app., like so:

    new ImageIcon(getClass().getResource("/src/Action-exit-icon.png")); 
    

    Overhauling the code, I get this:

    import java.awt.Color;
    import javax.swing.*;
    
    public class JavaGui148 {
    
        public JComponent getGUI() {
            JPanel p = new JPanel();
    
            p.setBackground(Color.GREEN);
    
            return p;
        }
    
        public JMenuBar getMenuBar() {
            /**
             * Menu Bar stuff
             */
            JMenuBar menuBar;
            JMenu menu;
            JMenuItem menuItem;
    
            // MENU BAR 
            menuBar = new JMenuBar();
            menuBar.setVisible(true);
    
            // MENU 1
            menu = new JMenu("File");
            menuBar.add(menu);
    
            // MENU 1 ITEM
            ImageIcon icon = new ImageIcon(getClass().getResource(
                    "/src/Action-exit-icon.png"));
            menuItem = new JMenuItem("Exit Program", icon);
            menu.add(menuItem);
    
            return menuBar;
        }
    
        public static void main(String[] args) {
            Runnable r = new Runnable() {
    
                @Override
                public void run() {
                    JavaGui148 gui = new JavaGui148();
    
                    JFrame f = new JFrame("Demo");
                    f.setJMenuBar(gui.getMenuBar());
                    f.add(gui.getGUI());
                    // Ensures JVM closes after frame(s) closed and
                    // all non-daemon threads are finished
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    // See http://stackoverflow.com/a/7143398/418556 for demo.
                    f.setLocationByPlatform(true);
    
                    // ensures the frame is the minimum size it needs to be
                    // in order display the components within it
                    f.pack();
                    // should be done last, to avoid flickering, moving,
                    // resizing artifacts.
                    f.setVisible(true);
                }
            };
            // Swing GUIs should be created and updated on the EDT
            // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
            SwingUtilities.invokeLater(r);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone explain why this works. I have 2 classes in Eclipse. A Class
Does Android emulator support OpenGl ES2.0? I run the code but Eclipse gives me
I am trying to run the following code from within Eclipse: Process process =
I want to run Eclipse Java Development Tools form source code, but I'm stuck
So I've got this code that compiles a class from a string and then
When I sbt run the following code, package com.example import java.io.ObjectInputStream import java.io.ObjectOutputStream import
I am trying to run this code below while learning the java sql stuff,
I just run the below code as a normal java class file and status.isReachable(3000)
I have a class called XMLtoXML.java and this is one of it's methods... import
I need an Eclipse Plugin run shell scripts under Linux. I have a beautiful

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.