Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access resources in JAR file?
    primarykey
    data
    text
    <p>I have a Java project with a toolbar, and the toolbar has icons on it. These icons are stored in a folder called resources/, so for example the path might be "resources/icon1.png". This folder is located in my src directory, so when it is compiled the folder is copied into bin/</p> <p>I'm using the following code to access the resources.</p> <pre><code> protected AbstractButton makeToolbarButton(String imageName, String actionCommand, String toolTipText, String altText, boolean toggleButton) { String imgLocation = imageName; InputStream imageStream = getClass().getResourceAsStream(imgLocation); AbstractButton button; if (toggleButton) button = new JToggleButton(); else button = new JButton(); button.setActionCommand(actionCommand); button.setToolTipText(toolTipText); button.addActionListener(listenerClass); if (imageStream != null) { // image found try { byte abyte0[] = new byte[imageStream.available()]; imageStream.read(abyte0); (button).setIcon(new ImageIcon(Toolkit.getDefaultToolkit().createImage(abyte0))); } catch (IOException e) { e.printStackTrace(); } finally { try { imageStream.close(); } catch (IOException e) { e.printStackTrace(); } } } else { // no image found (button).setText(altText); System.err.println("Resource not found: " + imgLocation); } return button; } </code></pre> <p>(imageName will be "resources/icon1.png" etc). This works fine when run in Eclipse. However, when I export a runnable JAR from Eclipse, the icons are not found. </p> <p>I opened the JAR file and the resources folder is there. I've tried everything, moving the folder, altering the JAR file etc, but I cannot get the icons to show up.</p> <p>Does anyone know what I'm doing wrong?</p> <p>(As a side question, is there any file monitor that can work with JAR files? When path problems arise I usually just open FileMon to see what's going on, but it just shows up as accessing the JAR file in this case)</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload