<p>When running a Java app from eclipse my ImageIcon shows up just fine.</p>

<p>But after creating a jar the path to the image obviously gets screwed up.</p>

<p>Is there a way to extract an image from the jar at runtime so I can then open it up?  Or, is there a better way to do this?</p>

<p>I'd like to distribute a single jar file if possible.</p>

[{'answer_id': 31146, 'author': 'Tim Frey', 'author_id': 1471, 'author_profile': 'https://Stackoverflow.com/users/1471', 'pm_score': 5, 'selected': False, 'text': '<p>You can try something like:</p>\n\n<pre><code>InputStream stream = this.getClass().getClassLoader().getResourceAsStream("/images/image.jpg");\n</code></pre>\n\n<p>In your JAR file, you might have a directory structure of:</p>\n\n<blockquote>\n  <p>MyJAR.jar<br>\n  - com (class files in here)<br>\n  - images<br>\n  ----image.jpg</p>\n</blockquote>\n'}, {'answer_id': 45580, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 6, 'selected': True, 'text': '<p>To create an <code>ImageIcon</code> from an image file within the same jars your code is loaded:</p>\n\n<pre><code>new javax.swing.ImageIcon(getClass().getResource("myimage.jpeg"))\n</code></pre>\n\n<p><code>Class.getResource</code> returns a URL of a resource (or <code>null</code>!). <code>ImageIcon</code> has a constructors that load from a URL.</p>\n\n<p>To construct a URL for a resource in a jar not on your "classpath", see the documentation for <code>java.net.JarURLConnection</code>.</p>\n'}, {'answer_id': 15353743, 'author': 'ekerner', 'author_id': 233060, 'author_profile': 'https://Stackoverflow.com/users/233060', 'pm_score': 4, 'selected': False, 'text': '<p>This is working for me to load and set the content pane background image:</p>\n\n<p>jar (or build path) contains:</p>\n\n<pre><code> - com\n - img\n ---- bg.png\n</code></pre>\n\n<p>java contains:</p>\n\n<pre><code>JFrame f = new JFrame("Testing load resource from jar");\ntry {\n    BufferedImage bg = ImageIO.read(getClass().getResource("/img/bg.png"));\n    f.setContentPane(new ImagePanel(bg));\n} catch (IOException e) {\n    e.printStackTrace();\n}\n</code></pre>\n\n<p>Tested and working in both jar and unjarred (is that the technical term) execution.</p>\n\n<p>BTW <code>getClass().getClassLoader().getResourceAsStream("/img/bg.png")</code> - which I tried first - returned me a null InputStream.</p>\n'}, {'answer_id': 34138083, 'author': 'Tobore Igbe', 'author_id': 5650771, 'author_profile': 'https://Stackoverflow.com/users/5650771', 'pm_score': 0, 'selected': False, 'text': '<p>Load image in from Jar file during run time is the same as loading image when executed from IDE e.g netbeans the difference is that when loading image from JAR file the path must be correct and its <b>case sensitive</b> (very important).\nThis works for me</p>\n\n<pre>\n image1 = new javax.swing.ImageIcon(getClass().getResource("/Pictures/firstgame/habitat1.jpg"));\n        img = image1.getImage().getScaledInstance(lblhabitat1.getWidth(), lblhabitat1.getHeight(), Image.SCALE_SMOOTH);\n        lblhabitat1.setIcon(new ImageIcon(img));\n</pre>\n\n<p>if p in "/Pictures/firstgame/habitat1.jpg" is in lower case it wont work. check spaces, cases and spelling</p>\n'}, {'answer_id': 42084902, 'author': 'Abdelsalam Shahlol', 'author_id': 7199218, 'author_profile': 'https://Stackoverflow.com/users/7199218', 'pm_score': 2, 'selected': False, 'text': '<p>In netbeans 8.1 what I\'ve done is to include the folder of icons and other images called <strong>Resources</strong> inside the <strong>src</strong> folder in the project file. So whenever i build <strong>Jar</strong> file the folder is included there.The file tree should be like this:</p>\n\n<blockquote>\n  <ul>\n  <li>src (Java files in source packges are here)</li>\n  </ul>\n  \n  <blockquote>\n    <ul>\n    <li><p>** PACKAGE YOU NAMED IN PROJECT**</p>\n    \n    <blockquote>\n      <ul>\n      <li><strong>file.java</strong></li>\n      </ul>\n    </blockquote></li>\n    <li><p>Resources</p>\n    \n    <blockquote>\n      <ul>\n      <li>image.jpg</li>\n      </ul>\n    </blockquote></li>\n    </ul>\n  </blockquote>\n</blockquote>\n\n<p>The code should be like:</p>\n\n<pre><code>jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/31127', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2598/']