开发者

Relative path does not work when jar run through command line but works when run through Netbean

开发者 https://www.devze.com 2023-03-03 04:26 出处:网络
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.

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.

Relative path does not work when jar run through command line but works when run through Netbean


Just drop the src from your path. Also, while loading jarred images, you should be using getClass().getResource()


  1. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消