user55924
user55924

Reputation: 142

Not able to link properties file in deployed java program

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 errorERROR

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.

Upvotes: 0

Views: 67

Answers (1)

user55924
user55924

Reputation: 142

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) );

Upvotes: 1

Related Questions