0

when i run this line:

URL myurl = getClass().getResource("/bla.xml");

I got myurl is null

if I try to debug in runtime this value "getClass().getResource("/bla.xml")" I got the right value. it seems that something went wrong with placement. what it can be? using eclipse oxygen

shira stenmetz
  • 297
  • 1
  • 6
  • 11

1 Answers1

0

If maven directory structure is used you can put the XML file in the resources folder. If resources folder is not available in the maven directory structure , you can create a one. The path of the resources folder are src/main/resources and src/test/resources. The XML file needs to be in the classpath. If you are running the jar, the XML file needs to be embedded in the jar file.

arunsrajan
  • 46
  • 2
  • I dont have the resource folder under src/main but it configure "use as a source folder". it is need to work? cause it is not:( – shira stenmetz May 27 '19 at 11:12
  • You will have to create resources folder and place the XML file in that folder. The files in the resources folder will be added to classpath. I think you have put the XML file in src/main. You should place it in src/main/java then it should work. – arunsrajan May 27 '19 at 11:57
  • tried it but it still getting null in the (only) url placement – shira stenmetz May 27 '19 at 12:20
  • It is working for me? How are you executing the code which has the statement you have given, Is it TestCase or main class? – arunsrajan May 27 '19 at 13:12
  • Can you place the XML file in src/test/java and try once. Also please check whether the eclipse menu project->Build Automatically is checked. – arunsrajan May 28 '19 at 11:30
  • I see that the value of getClass().getResource() is the path for target/classes/bla.xml. I try to understand how my resources needs to arrive to target folder? – shira stenmetz May 29 '19 at 06:20
  • You need to see the bla.xml in test-classes i.e target/test-classes/bla.xml since you are using junit test cases for executing the statement. Either you need to check the build Automatically option or you need to clean the project every time when you create a new resources in maven project if Build Automatically is not checked. – arunsrajan May 30 '19 at 05:52