12

I am building my Spring Boot application using Maven, so I can start it with:

java -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod

I want to have a directory first on the classpath that would allow me to place some files on the filesystem without having to unzip the jar to change them.

I have tried using loader.path, but it does not seem to work.

java -Dloader.path="config/*" -jar myjar-1.0-SNAPSHOT.jar --spring.profiles.active=prod

The config dir is a subdirectory of where the jar is located. I am trying to load a keystore file which is injected as a Resource in my application. There is such a file in the src/main/resources, but that only works in my IDE, not when packaged as a jar. So I want to put a file first on the classpath so that that one is found first on the classpath.

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211

1 Answers1

13

You can use loader.path but only if the Main-Class is PropertiesLauncher (so it depends how you built the JAR file). Maybe you need to re-build the JAR with packaging=ZIP in the Boot plugin (e.g. docs here)? Can you not set the path to the keystore as a "file:" URL?

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
  • 1
    Using `file:` does fix it and I can use it as a workaround. However, using `ZIP` in the Maven plugin does not put it on the classpath and I would really like to get that working. I get `NoClassDefFoundError: org/springframework/transaction/PlatformTransactionManager` when I add the `loader.path` – Wim Deblauwe May 02 '14 at 13:43
  • 1
    That's because you are removing the default loader.path. You would need "loader.path=lib/,config/" to use both. – Dave Syer May 02 '14 at 14:12
  • Actually the other way round (given you are asking for "config/" to be first). – Dave Syer May 02 '14 at 14:19
  • 1
    Ah! I thought `loader.path` was to put _additional_ things on the classpath. – Wim Deblauwe May 05 '14 at 07:02
  • 1
    Does Spring Boot provide some way to add items to the classpath as opposed to replacing it entirely as it does when setting `loader.path`? – Derek Mahar Nov 22 '16 at 21:02
  • No. But if the default is a directory, it's hardly difficult to add it back in, is it? – Dave Syer Nov 23 '16 at 07:58
  • Faced similar issue. using `ZIP` in the Maven plugin fixed the issue. Thanks! – Loganathan Sep 06 '20 at 07:52