0

I have a maven project which I develop using intellij IDEA. I have specified a resource directory in intellij which contains some configuration files and data files. When I run the program in intellij, there is no issue, but I would like to access these files when I run the jar-file from command line also, and I do not know how to specify for maven that a directory is a resources directory without it being copied into the jar-file, which I do not want. Is there a flag to java to point out the resources directory?

Edit: To be clear, I have my jar file and I want to access a resource directory located outside my jar file using the classLoader. However, it seems as I cannot use -classpath to specify where the classLoader should look, as there is no difference in behaviour.

I can of course open a normal text file, but I would rather have it work from the classLocader machinery.

MatsB
  • 81
  • 6
  • create your resource folder as @Antoniossss say, then right click on it. go to `Mark Directory as -> Resources Root and` it work. – amir110 Dec 17 '17 at 11:15
  • Yes, it works when I run it from the IDE, but I need to be able to change the files after I have created the jar, so the directory has to live somewhere outside the jar. – MatsB Dec 17 '17 at 11:51
  • Possible duplicate of [Reading a plain text file in Java](https://stackoverflow.com/questions/4716503/reading-a-plain-text-file-in-java) – Antoniossss Dec 17 '17 at 12:11

1 Answers1

0

If you are using Maven stucture you shoud have 2 directories - java/main/src with sources and java/main/resources with app resources. Both are copied into JAR

Antoniossss
  • 24,977
  • 3
  • 43
  • 86
  • Thanks, but I do not want it copied. I need to be able to change input data and configuration files from outside the jar. – MatsB Dec 17 '17 at 11:50
  • Then move it somewhere else and write apropriate code to use external files. – Antoniossss Dec 17 '17 at 11:51
  • OK, I thought there was a way to tell the jvm that a directory is a resources directory so that I can tell the location at run-time. I would have liked to avoid fixing the location in the code. Thanks anyway. – MatsB Dec 17 '17 at 15:29
  • There is a way - put required directory on the classpath with `-cp` switch. – Antoniossss Dec 17 '17 at 19:44
  • Tried that earlier, no difference in behaviour. Somewhere I read that when running with -jar, the classpath paramter to java is ignored. – MatsB Dec 17 '17 at 19:50
  • @MatsB indeed. So don't run with -jar. Run with -cp. – JB Nizet Dec 17 '17 at 20:05
  • @Antoniossss the directories are src/main/java and src/main/resources. – JB Nizet Dec 17 '17 at 20:05
  • @JBNizet, thanks, but how can I then specify the jar file as the executable? – MatsB Dec 17 '17 at 20:29
  • You dont. Do `java -cp jarfile.jar full.qualified.name.of.class.with.main`. And include that magic directory of yours in CP – Antoniossss Dec 17 '17 at 20:31