user1914867
user1914867

Reputation: 53

Not able to access the image file when running jar file outside Netbeans

lblImage = new javax.swing.JLabel();
lblImage.setIcon(new javax.swing.ImageIcon("E:..path...png"));

I added the file like this. I know it is not accessing the path when running the jar file. Help me how to import a image file to java project in Netbeans.

Upvotes: 3

Views: 1449

Answers (2)

Adesh singh
Adesh singh

Reputation: 2133

Create the image folder in the src folder, put the images in the folder and give the relative path for creating the image icon object

     jLabel1.setIcon(new ImageIcon(getClass().getResource("/image/img1.jpg")));

then rebuild the jar .it will access the images from the jar

Upvotes: 1

Chandana
Chandana

Reputation: 2638

Recommended way is place images inside resource folders and use it as below:

jLabel1.setIcon(new ImageIcon(getClass().getResource("/path/to/image.png"))); 

Read More: NetBeans Doc

Upvotes: 6

Related Questions