0

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.

Community
  • 1
  • 1
user55924
  • 142
  • 16
  • Possible duplicate of [Getting absolute path of a file loaded via classpath](http://stackoverflow.com/questions/8136891/getting-absolute-path-of-a-file-loaded-via-classpath) – tima May 09 '17 at 12:49
  • 2
    The `src` folder won't and shouldn't be there at deployment time. Put somewhere that will be included in the deployment, e.g. a `resources` directory. And resources are not files. You need to use `Class.getResource()` and friends. not `File`. – user207421 May 09 '17 at 12:54
  • 1
    See also http://stackoverflow.com/documentation/java/2433/resources-on-classpath#t=201701111734535687024&a=remarks . – VGR May 09 '17 at 16:21
  • @EJP but i want to create a file when it don't exists because my program deletes some files after exiting and recreates it on startup because of some security reasons .. Can you give an example? – user55924 May 09 '17 at 16:55

1 Answers1

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

user55924
  • 142
  • 16