0

I compiled my code into a jar file that should read some resource files stored in a local drive in windows. I am using:

InputStream inputStream = NewMain.class.getResourceAsStream("/com/companies.json");

this is working but (I think) the resource is getting compiled inside the jar file. If I delete the resource files from the drive after building the project, the jar still finds the files. So in that case, always that I want to modify/add/remove the resource files I have to build the project again? I would like to have a .jar that reads files that are stored localy in my computer. Does anybody have a suggestion what could I use?

pablo casanovas
  • 87
  • 2
  • 13

1 Answers1

0

The question doesn't say which platform - but say you were on a unix or bsd platform $JAVA_HOME/bin/jar -tvf YOURJARMNAME.jar lists all the files in the JAR - to confirm whether the resource is there or not.

If you want to open a specific file either use Paths.getPath or new File() to specify where it is and then use any of the methods in answers to Reading a plain text file in Java

Mr R
  • 680
  • 2
  • 15
  • this is not correct. You can not use File() if you want to run a JAR. See also https://stackoverflow.com/questions/10144210/java-jar-file-use-resource-errors-uri-is-not-hierarchical – pablo casanovas Mar 21 '21 at 14:14
  • @pablocasanovas the OP isn't about getting resources inside the JAR, they are putting files somewhere else and loading them.. Yes that wouldn't work in an app engine, but if they used the jar like this `java -cp their.jar runningClass` it would. – Mr R Mar 21 '21 at 19:19