0

There is binary file for update my switch configuration in src/main/resources directory in run time. when i try it with debug mode in eclipse the file exist and there is no problem, but when i create an executable jar file for real application this file does not exist.

Where is the problem?

String filePath  = new File("/resources/firmwares/myConfigFile.bin").getAbsolutePath();

File f = new File(filePath);
    if (f.exists()) 
            upload();
enes_k
  • 1
  • 1
    This has about 1000 answers on SO, [here is one](http://stackoverflow.com/questions/574809/load-a-resource-contained-in-a-jar). – Stefan Jun 23 '16 at 11:36
  • There is no such thing as an 'embedded file'. There are files, and there are resources. Different things. – user207421 Jun 23 '16 at 12:35

1 Answers1

0

Assuming that you are using Maven and you added the resource plugin so during maven build it will copy your resources to the target dir and then during packaging it will also package them

Once your JAR is created you will need to use another method to access your file since now your file is packaged within the JAR file

To access a file that is located inside a JAR file use:

InputStream is = this.getClass().getClassLoader().getResourceAsStream("firmwares/myConfigFile.bin");

Or

URL url = this.getClass().getClassLoader().findResource("firmwares/myConfigFile.bin");
shachar
  • 641
  • 5
  • 12