0

My file is under project/src/test/resources/fileToBeFound.xml and src/target/test-classes/fileToBeFound.xml. In my class, I'm using

this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath() 

to get the relative path which is /project/target/classes. Then I convert it to /project/src/test/resources. In my local, everything is going right. but when I run the CI job, there's an IO Exception.

Edmond
  • 614
  • 2
  • 11
  • 26
  • 1
    what happens if you do `this.getClass().getResource("/fileToBeFound.xml").getPath()` instead of your potentially `SecurityException` throwing version... and more to the point why not just `this.getClass().getResourceAsStream("/fileToBeFound.xml")` so that you don't face the many problems associated with trying to convert a `URL` to a `File`? – Stephen Connolly Sep 09 '13 at 13:03
  • @StephenConnolly, Thanks for your reply. Actually this.getClass().getResource("/fileToBeFound.xml").getPath() works fine if this is called within the same project. But I have another project say ProjectB depend on this and also needs to access "/fileToBeFound.xml". Obviously I don't want to create a duplicate "/fileToBeFound.xml". In this case, this.getClass().getResource("/fileToBeFound.xml").getPath() returns a null. The reason might be that the file is not in ProjectB's classpath. Any suggestion towards that? – Edmond Sep 09 '13 at 17:26
  • Use a class that is on the classloader you want rather than `this` (you may need a test scoped dependency) – Stephen Connolly Sep 09 '13 at 19:14
  • I've got the same problem. How do I get a class from a differen classloader. Can you give an example? instead of `this.getClass().getResource("/fileToBeFound.xml").getPath()` i tried `differentClassFromSameProject.getClass().getResource("/fileToBeFound.xml").getPath()`, but it made no difference. – MrLang Jun 08 '15 at 09:22

0 Answers0