Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems like there are two questions here:</p> <ol> <li><p>How do I get NetBeans to include an image file in the jar produced when I build my project?</p></li> <li><p>How do I access an image file from a jar?</p></li> </ol> <p>This answer applies to NetBeans 6.8 and addresses both of the subquestions.</p> <p>Assume that you have an ant based Java Application Project.</p> <p>Here is the 'Files' view of the project</p> <pre><code>JP + images + test.jpg + nbproject + src + jp + Main.java + test + build.xml + manifest.mf </code></pre> <p>Inside your Main.java you have code like this:</p> <pre><code>public static void main(String[] args) throws IOException { // find the file in the file system.. probably not a good idea File f = new File("images/test.jpg"); System.out.println(f.getCanonicalPath()+" "+f.exists()); </code></pre> <p>When you run this project from inside NB you get this output:</p> <pre><code>/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true </code></pre> <p>When you run the code packed into the jar, you get something like this:</p> <pre><code>bash-3.2$ pwd /export/home/vkraemer/nbhg/web-main bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar /export/home/vkraemer/nbhg/web-main/images/test.txt false </code></pre> <p>To get something better when the jar is executed, you need to do the following:</p> <p><strong>Add the images directory as a source root for you project</strong>.</p> <p>Right click on the project and select the Properties item. A dialog will appear.</p> <p>Select 'Sources' in the list that is on the left side of the dialog. This will change the content of the panel on the right side of the dialog.</p> <p>Press the 'Add Folder...' button that appears next to the 'Source Package Folders' table. A FileChooser will appear.</p> <p>Use this chooser to select the images folder and press the OK button. An entry for the images folder will be added table.</p> <p>Use the OK button on the Project Properties dialog to accept the changes and dismiss the dialog.</p> <p><strong>Change your code to use <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Class.html#getResource%28java.lang.String%29" rel="noreferrer">Class.getResource()</a></strong>.</p> <pre><code>public static void main(String[] args) throws IOException { // find the file in the file system.. probably not a good idea File f = new File("images/test.jpg"); System.out.println(f.getCanonicalPath()+" "+f.exists()); URL url = Main.class.getResource("/test.jpg"); System.out.println(url); </code></pre> <p>When you run the project from inside the IDE, you should see something like this:</p> <pre><code>/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg true file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/images/test.jpg </code></pre> <p>When you run the code packed into the jar, you will get something like this:</p> <pre><code>bash-3.2$ pwd /export/home/vkraemer/nbhg/web-main bash-3.2$ java -jar /export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar /export/home/vkraemer/nbhg/web-main/images/test.jpg false jar:file:/export/home/vkraemer/NetBeansProjects/JavaApplication2/dist/JavaApplication2.jar!/test.jpg </code></pre> <p>After you get the URL for the test.jpg file, you can use <a href="http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.net.URL%29" rel="noreferrer">ImageIcon(URL)</a> to create the icon</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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