1

I am newbie in XML files operation. My program should do that when it is executed, it must generate random numbers and write to a XML file. Every time when I debug it in NetBeans IDE everything is okay, but when I try execute it in console with "Java -jar blablabla.jar" the XML files are not changing and I still see old values in XML files. Why is that happen? Why can IDE modify the values in XML while console cannot?

Here is my XML operations code block:

public class CreateXML {

public CreateXML(ArrayList<Integer> array,String name){
    try{
    DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=dbf.newDocumentBuilder();

    String path="file"+name+".xml";
    File xmlfile=new File(path);
    if(xmlfile.exists()){
        xmlfile.delete();
        System.out.println("There was this file and it is deleted.");
    }

    Document doc=db.newDocument();

    Element rootElement=doc.createElement("numberList");
    doc.appendChild(rootElement);
        for (int i = 0; i < array.size(); i++) {
            Element staff=doc.createElement("number");
            rootElement.appendChild(staff);

            Attr attr=doc.createAttribute("id");
            attr.setValue(Integer.toString(i));
            staff.setAttributeNode(attr);
            staff.setTextContent(Integer.toString(array.get(i)));
        }

    TransformerFactory tf=TransformerFactory.newInstance();
    Transformer t=tf.newTransformer();

    DOMSource source=new DOMSource(doc);
    StreamResult result=new StreamResult(xmlfile);

    t.transform(source, result);
    }catch(Exception e){
        e.printStackTrace();
        }
    }

}
Dylan Wheeler
  • 6,928
  • 14
  • 56
  • 80
umitkilic
  • 327
  • 1
  • 4
  • 17
  • 1
    The created file may not be where you think it is, try to print `xmlfile.getAbsolutePath()` – Arnaud Jul 20 '16 at 12:19
  • you are sure you running the same Version of your code if you run it from IDE and from jar? – Jens Jul 20 '16 at 12:19
  • @Berger i think it is not the problem point because IDE can find the file but .jar file cannot. So, I mean if the path is not correct IDE cannot find the file too, right? – umitkilic Jul 20 '16 at 12:26
  • @Jens Yes I am sure versions are the same. Because I click "Clean and Build Project" button before trying to execute from console. – umitkilic Jul 20 '16 at 12:28
  • @umtklc : You are using a relative path (since you don't indicate an absolute path for the file), I'm pretty sure the final absolute path won't be the same in the IDE and from the console. – Arnaud Jul 20 '16 at 12:30
  • @Berger So you think I should use xmlfile.getAbsolutePath() function ? Exactly where should I use it in code block? – umitkilic Jul 20 '16 at 12:32
  • 1
    `xmlfile.getAbsolutePath()` was just to demonstrate that the paths won't be the same.You may create your file with an absolute path, e.g `File xmlfile=new File("C:\\\\temp\\"+"file"+name+".xml");` – Arnaud Jul 20 '16 at 12:35
  • @Berger Actually i will sent the jar file someone else, so he should find easly the file and i should be sure about existing the directory that i point. Sure about temp but maybe should find more easier place. How can i determine the place that .jar file is located ? – umitkilic Jul 20 '16 at 12:44
  • @Berger i will get over with that code " jarpath=CreateXML.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath().toString();" thanks for all comments. – umitkilic Jul 20 '16 at 12:51
  • Yes I was about to link this topic :) : http://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file , – Arnaud Jul 20 '16 at 12:52

0 Answers0