Not able to link properties file in deployed java program

这一生的挚爱 提交于 2019-12-12 04:47:16

问题


I just finished my project everything is working fine but when i deployed it to .jar it was not showing images, so i searched over the web and found a solution initially i was using this

File file = new File("src/sample/Drawables/1.jpg");
Image image = new Image(file.toURI().toString());


Imageview.setImage(image);

This was working good when i was simply building through the IDE are running the application.

But when i tried to run after deploying the application the image was not shown so i found a solution

Image image = new Image(String.valueOf(getClass().getClassLoader().getResource("src/sample/Drawables/1.jpg")));
        Imageview.setImage(image);

after doing this i the image was shown in the deployed application.

So i tried simmiler approch for prooperties file initily the code was

            File file = new File("src/sample/PropertiesMain/Alpha.properties");


        if(!file.exists()){
            file.createNewFile();
        } 

So again this code was working fine until i deployed my application to jar file

the error given was java io exception cannot find file specified .

the i foolishly applied same approch as i did for image.

File file = new

File(String.valueOf(getClass().getClassLoader().getResource("src/sample/PropertiesMain/Alpha.properties")));


                if(!file.exists()){
                    file.createNewFile();
                }

And obviously got this error

i need some help that how can i get that properties file!

please ask if you need any additional information !!

i am explaining that my question is not a duplicate as it is not asking about getting absolute path or anything my program was working good until i deployed it. And i was looking help for because students usually don't know the absolute reasons of errors. And not going to search something like This By the way that question is not explaining anything close what i was asking in my question.


回答1:


I found my answer in this link this is what i should use to read a file from the jar

InputStream is = myClass().getResourceAsStream("res/usernames.txt"); reader = new LineNumberReader( new InputStreamReader(is) );



来源:https://stackoverflow.com/questions/43870215/not-able-to-link-properties-file-in-deployed-java-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!