0

I have a SPRING 2.5.6 based project and my properties file is outside executable Jar file like

./ |---MyApplication.Jar |---MyApplication.properties

I don't know how to configure PropertyPlaceholderConfigurer so that it can pick the file outise of JAR file or any other way by which spring can know the location of properties file. In other words How to put Jar root path in class-path because spring can automatically pick the properties file from class-path.

I have read the following Questions but it did not exactly tell how to configure PropertyPlaceholderConfigurer.

Read properties file outside JAR file

Add properties file to build path of runnable jar

java -jar -cp . ./main.jar I know this way of adding root path into class-path but my client do not want to run jar by command line or batch file. So is there a way to configure Spring somehow?

I think of possible solution is to make JAVA base configuration alongside xml base configuration.

Community
  • 1
  • 1
Mubasher
  • 943
  • 1
  • 13
  • 36
  • Spring 2.5.6? WTF? Are you aware they are about to release Spring 5.0??? – Sean Patrick Floyd Mar 13 '17 at 10:27
  • @Sean Patrick Floyd Yes I am aware of this, Its a lagacy application made in 2005 or 2006. Upgrading to latest versions result into catastrophic failure because in such case many other dependent components will need to change and that is huge effort. So We are bearing this. :-) – Mubasher Mar 13 '17 at 10:54

3 Answers3

0
java -jar -cp . ./main.jar

Change run command.

pesky
  • 1
  • Sir I already know this method and I also put the reference of other questions which suggested the same method but I have to run the jar as double click, I cant instruct my customer to run jar by command line. so i need a solution which works without command line. – Mubasher Mar 13 '17 at 07:13
  • As I said, My properties file is not in class-path. it is outside of class-path. – Mubasher Mar 13 '17 at 08:49
  • Use OS classpath. or, You can also use the OS environment variable. – pesky Mar 13 '17 at 08:55
  • Can you please explain what is OS classpath? if you are referring environment variable than there is a problem, i have many clients who will use my jar, how can I put environment variable of every client , I don't think so this is a very feasible solution in deployment phase. – Mubasher Mar 13 '17 at 09:10
0

Solution- To get the directory where jar is located, spel can be used. The following should get you going.

  <context:property-placeholder location="file:///#{T(java.lang.Object. getClass().getProtectionDomain().getCodeSource().getLocation‌​())}/ application.properties"/>
pkoli
  • 646
  • 8
  • 21
  • What will be the packaging of outer project? I don't think so your solution ever work, As I said properties File is outside of resource folders, I have to give properties file along with jar file so that my client can configure application and can restart the application after configuration. – Mubasher Mar 13 '17 at 07:39
  • This can be used to configure spring to read external properties file. – pkoli Mar 13 '17 at 07:47
  • How to give ${ext.prop.dir} path ? Can you update your answer according to this After Test, if It worked I will accept this. – Mubasher Mar 13 '17 at 07:59
  • Problem is ${ext.prop.dir} has to be dynamic and has to be root path of JAR file. I don't know what will be the directory in which it is placed on client system. How to give this path in another properties file. i mean root path of executable jar. – Mubasher Mar 13 '17 at 09:23
  • This provides path of the jar, in your jar where you're providing path to properties file, put the below mentioned line there followed by name of properties file- getClass().getProtectionDomain().getCodeSource().getLocation() – pkoli Mar 13 '17 at 09:31
  • Have you ever configure spring with XML? How Can I give getClass().getProtectionDomain().getCodeSource().getLocation‌​() in Spring XML configuration file? – Mubasher Mar 13 '17 at 09:35
  • Using spel #{T(java.lang.Object. getClass().getProtectionDomain().getCodeSource().getLocation‌​())} – pkoli Mar 13 '17 at 09:55
-2

You can use Maven or Gradle to manage your project, he can help you automatically add dependencies, you do not need to manually import Jar package.