0

I need to pass the file path to a function of an external class which uses it in the following way:

byte[] keyBytes = Files.readAllBytes((new File(pathname)).toPath());

The class cannot be edited otherwise I would have used this method to read the file ->

InputStream inputStream = getClass().getResourceAsStream("/file_name.xyz");

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

When running the code locally, it's able to retrieve the file but when the JAR is deployed on cloud it's failing to find it.

Currently I have pasted the file at src/main/resources but I can shift it to any other folder also. Just need to pass the path so that the function reads it.

Can someone help me with this? I'm stuck on it from couple of days

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Raj
  • 1
  • 2
    You can't. A file in a jar is not a file; it is impossible to read it with `new File(str)`. Either [A] double your efforts to fix the silly code in the external thing, or [B] find a place you can write files, create a new file (Using `getResourceAsStream` and then `.transferTo`, for example), give the path to _that_, delete it when you're done. – rzwitserloot Sep 16 '22 at 09:21
  • You need to find the [current directory](https://technojeeves.com/index.php/aliasjava1/91-find-the-current-directory-in-java) and work it out from there – g00se Sep 16 '22 at 09:22
  • https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar – Simon Martinelli Sep 16 '22 at 09:38
  • May need application restart if the configuration does not scan for new packages. See the cloud config info. – Samuel Marchant Sep 16 '22 at 11:43

0 Answers0