I have added "Images" folder inside "src" folder in Java project.
For setting top left image on title bar I am using image from Image folder.
To do that I have written code
Image img=Toolkit.getDefault开发者_运维问答Toolkit().getImage("src/Images/Sell Smart2 copy.jpg");
frame.setIconImage(img);
This works fine when project run through Netbeans But when I run jar of that project through command line using command "java -jar project.jar" then Image is not shown.
Why this problem coming ? Any solution for that
Thanks, Image added for refernce.
Just drop the src from your path. Also, while loading jarred images, you should be using getClass().getResource()
- have you checking src folder is included in jar when you create a jar? use jar -xf or similar option to see jar contents or use winrar to see jar contents.
In jar you can use
getClass().getClassLoader.getResourceAsStream();
And when you get the stream, you can do anything you wanted.
But remember when using resourseStream while you are running you project in netbeans may be it will not work but when you make jar it'll work.
精彩评论