4

I need to save my XML file in the same location where my Java file is present and call and use the XML when java file is executed. Is there any way through which this can be achieved?

Following is my code:

File fXmlFile = new File("C:\testing eclipsefolder/workspace/java1`/bin/diliya123/v1.xml");

Here I need to specify the file path of my XML. But I don't want to give that path, it should be such that when i give my jar file to others it shouldn't change its path should be constant every time.

When I try to run the same script using the above code I get the following type of error:

[TestNG] Running:
  C:\Documents and Settings\saalam\Local Settings\Temp\testng-eclipse-1407458964\testng-customsuite.xml

Guys please suggest me someways through which it is possible.

Sufian
  • 6,405
  • 16
  • 66
  • 120
autoD
  • 109
  • 2
  • 14
  • "\" is reserved to escape things in String if you need a "\", you have to escape it, like this "\\" –  Aug 11 '15 at 05:32
  • see http://stackoverflow.com/questions/574809/load-a-resource-contained-in-a-jar for your xml loading issue –  Aug 11 '15 at 05:34
  • can any one tell me why i was getting that error,when i tried to execute full the java code with full XML path. now that i have changed the path,i am still getting same error. – autoD Aug 11 '15 at 06:08

3 Answers3

1

Try using a relative path instead of an absolute path, like below:

File fXmlFile = new File("bin/diliya123/v1.xml");
uoyilmaz
  • 3,035
  • 14
  • 25
  • i tried running my code as you said, but its not working out.its giving me following error `java.io.FileNotFoundException: C:\testing eclipsefolder\workspace\java1`\.\diliya123\v1.xml (The system cannot find the path specified)` – autoD Aug 11 '15 at 05:40
  • 1
    Ok it should be File fXmlFile = new File("bin/diliya123/v1.xml"); – uoyilmaz Aug 11 '15 at 05:46
0

You mentioned about giving your jar file to "others". In that case, if the XML file is bundled in the jar, something like this may do what you want
File fXmlFile = new File(getClass().getClassLoader().getResource("YourXmlFile.xml").getFile());

Even if the XML file can't be bundled as part of the jar, tell your users to add the location of the XML file on their file system to the CLASSPATH of the calling program and it should work.

sranjan
  • 11
  • 1
0

If you are going to put your jar and xml files in the same place (folder), then you can simply give path like this.

File xml_file = new File("v1.xml");

In my case, my application requires bunch of xml files. So, i will keep all my xml files in a separate directory as xml/ and passing the path as

File xml_file = new File("xml/v1.xml");

The directory structure will be
enter image description here Application_folder
         enter image description here application.jar
        enter image description here xml
            enter image description here v1.xml
            enter image description here v2.xml
            enter image description here v3.xml

When ever you run the jar, it will access files from the xml folder.

Reddi Rajendra P
  • 704
  • 9
  • 25