0

Hi I am working on a Maven project having dependency on a external jar which has a class ConfigLoader having following loader() method.

public class ConfigLoader {
   public void initialize() {
      loader();
   }
   private static void loader() {
      URL configURL = ConfigLoader.getClass().getResource("runtimeConfiguration.xml");
      //some other method calls to which configURL is an argument.
   }
   //other methods of ConfigLoader class
}

and the directory structure is like this -

src
|...main
|.......java
|.......resources
|................dev
|................prod

both dev and prod have a file named runtimeConfiguration.xml and the code which uses this class is

public class Application {
   private Application application;
   public static void main(String []args){
      application = new Application();
      application.invokeConfigLoader();
      //additional code
   }
   private void invokeConfigLoader() {
      configLoader.initialize(); 
   }
}

The error I get is

could not find: runtimeConfiguration.xml
and the exception is thrown at the getResource() line in the class from jar.

I have tried adding the dev folder to classpath but still the same error. I want to run this code from linux terminal, and the command I am giving from trunk directory (where all my exernal jars and resource folder sits after maven build) is -

java -cp /resources/dev/*:configuration-loader.jar 

I am using intelliJ 2017.2 and also tried to add the resources/dev folder as module dependency, but I keep on getting the same error. The resources folder is added as a library via project structure settings. I tried to search a lot but have not found any question with this issue. Kindly help me out as I am new to this environment based development. Thanks!

humblecoder
  • 137
  • 1
  • 7

1 Answers1

0

ConfigLoader.getClass().getResource("runtimeConfiguration.xml"); will try to get runtimeConfiguration.xml from the same package a the ConfigLoader is defined and not from the root of classpath. Try appending / to runtimeConfiguration.xml.

This should work ConfigLoader.getClass().getResource("/runtimeConfiguration.xml"); or ConfigLoader.getClass().getResource("/dev/runtimeConfiguration.xml"); depending how you are adding resources to your classpath.

See javadoc for more.

tsolakp
  • 5,858
  • 1
  • 22
  • 28
  • thanks for replying. The code which invokes this getResource is coming from an external jar, so I can't make any changes there. – humblecoder Dec 26 '17 at 22:13
  • Then, try to put the file under the path same as package name of `ConfigLoader`. For example if `ConfigLoader` is under `com.yourcompant.config` then put `runtimeConfiguration.xml` under `com/yopurcompany/config` directory. – tsolakp Dec 26 '17 at 22:22
  • one query I have, if the config sits in say `src/main/java/com/yourcomapany/config` and the `runtimeConfiguration.xml` sits in `src/main/java/resources` where the `resources` directory is marked as resources root. Then should it work? – humblecoder Dec 26 '17 at 22:30
  • It should then sit in `src/main/java/resources/com/yourcomapany/config`. – tsolakp Dec 26 '17 at 22:31
  • Hi both the solutions are not working. Thanks for being kind to reply though. – humblecoder Dec 26 '17 at 22:38
  • btw is there difference between `ConfigLoader.class.getClassLoader().getResource("runtimeConfiguration.xml");` and `URL configURL = ConfigLoader.getClass().getResource("runtimeConfiguration.xml");` ? – humblecoder Dec 26 '17 at 22:41
  • No. They are same. From javadoc: "This method delegates to this object's class loader." – tsolakp Dec 26 '17 at 22:42
  • https://stackoverflow.com/questions/676250/different-ways-of-loading-a-file-as-an-inputstream This discussion led me into thinking they are different. – humblecoder Dec 26 '17 at 22:45
  • Yes, if you are talking about how they treat the path. – tsolakp Dec 26 '17 at 22:52