11

I have written some code and exported it as a jar file. In this jar there is a file named automation.properties with defaults that I'm loading using

val automationPropertiesFileURL = getClass.getResource("/automation.properties")  
  if (automationPropertiesFileURL != null) {
    val source = Source.fromURL(automationPropertiesFileURL)
    config = new Properties()
    config.load(source.bufferedReader())
  }

But when this jar file gets added as a gradle dependency in C:\User\abc\.gradle and I want to read automation.properties from my current project, how can I override the location and read the file from my project and not from the jar file itself?

Swapnil Kotwal
  • 5,418
  • 6
  • 48
  • 92
  • for starters, do not include the default properties file in your jar. Just make sure there is a properties file in your classpath when you use the jar. – RaGe Jul 28 '16 at 15:10
  • Yes, we do have tried that too but no luck – Swapnil Kotwal Jul 31 '16 at 04:29
  • What does it mean it gets added as a gradle dependency? Do you add this jar as a dependency to other project? – Opal Aug 03 '16 at 06:42
  • Yes, this jar is not part of my current project. it is external jar added `C:\User\\.gradle` path and it pick-up relative path to that directory – Swapnil Kotwal Aug 03 '16 at 10:20

3 Answers3

5

The class loader will load the file from the location it finds first.

In your case, the file exists in two places:

  • Inside the current project itself
  • Inside a jar dependency

Which file will be found by the class loader, depends on the ordering of the "current project" and the jar dependency on the classpath. That's what you need to review, that's the key to loading the right file.

Your current code is correct as it is, this is a matter of classpath configuration.

janos
  • 120,954
  • 29
  • 226
  • 236
  • How do you run your program? – janos Aug 04 '16 at 09:16
  • Through eclipse gradle plugins, `clean build` – Swapnil Kotwal Aug 04 '16 at 09:19
  • Look into the classpath settings, and the ordering of dependencies. Make the project itself come higher than the dependencies. – janos Aug 04 '16 at 09:21
  • I didn't get that, my all jars are inside `C:\User\abc\.gradle` directory – Swapnil Kotwal Aug 04 '16 at 09:27
  • When you run a Java program, the list of libraries used are configured on the classpath, either on the command line or in the manifests of a jar file. For example `java -cp lib1.jar:lib2.jar com.example.MyClass`. The properties file can be present in both of these jars, but the classloader will find only the first one. By rearranging the classpath, in this example to `java -cp lib2.jar:lib1.jar com.example.MyClass` a different file will be loaded. You need to find how the classpath is set and adjust it accordingly, with the ordering that you need. – janos Aug 04 '16 at 09:35
2

I think

Source.fromInputStream(
  getClass.getClassLoader.getResourceAsStream("/automation.properties")
)

should work.


API Docs

Source#fromInputStream

Class#getClassLoader

ClassLoader#getResourceAsStream

Martijn
  • 2,268
  • 3
  • 25
  • 51
2

Java and so does SCALA, have different ways of reading properties file, in one of my answers I explain the difference between reading from properties file inside the Jar and properties file in a disk location.

See my answer HERE: Loading Properties from a JAR file (java 1.6)

This will work for JAVA and for SCALA too! (Note, for SCALA you could change basic sintax but same concept)

Hope it helps!

Community
  • 1
  • 1
Cesar Villasana
  • 681
  • 3
  • 6
  • Sir, your answer is my question, I have asked same thing which unfortunately not working. `config = new Properties() config.load(source.bufferedReader())` – Swapnil Kotwal Aug 02 '16 at 06:07
  • My answer says that you can load a property file from whenever you want using this (JAVA): `Properties properties = new Properties(); properties.load(new FileInputStream("extenalPropsFileLocation"));` Where "extenalPropsFileLocation" is the absolute path to your automation.properties – Cesar Villasana Aug 03 '16 at 15:43