title: JAXB Java Marshalling error
body: <p>I am trying to marshal a simple Java class with JAXB and I am running into errors. I am looking for advice on fixing the errors bellow
  These are the errors:</p>
  
  <pre><code>javax.xml.bind.JAXBException
   - with linked exception:
  [java.io.FileNotFoundException: C:\file.xml (Access is denied)]
      at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
      at JAXBExample.main(JAXBExample.java:23)
  Caused by: java.io.FileNotFoundException: C:\file.xml (Access is denied)
      at java.io.FileOutputStream.open(Native Method)
      at java.io.FileOutputStream.&lt;init&gt;(Unknown Source)
      at java.io.FileOutputStream.&lt;init&gt;(Unknown Source)
      ... 2 more
  </code></pre>
  
  <p>The errors come when I attempt to run the class for JAXB marshaling. It was downloaded from a tutorial website (<a href="http://www.mkyong.com/java/jaxb-hello-world-example/" rel="nofollow">http://www.mkyong.com/java/jaxb-hello-world-example/</a>) so It should be correct. </p>
  
  <p>This is the Marshal class:</p>
  
  <pre><code>import java.io.File;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  
  public class JAXBExample {
      public static void main(String[] args) {
  
        Customer customer = new Customer();
        customer.setId(100);
        customer.setName("mkyong");
        customer.setAge(29);
  
        try {
  
          File file = new File("C:\\file.xml");
          JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
          Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
          // output pretty printed
          jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
          jaxbMarshaller.marshal(customer, file);
          jaxbMarshaller.marshal(customer, System.out);
  
            } catch (JAXBException e) {
          e.printStackTrace();
            }
      }
  }
  </code></pre>
  
  <p>The Marshaling class references a Customer class. 
  This is the customer class:</p>
  
  <pre><code>import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Customer {
  
      String name;
      int age;
      int id;
  
      public String getName() {
          return name;
      }
  
      @XmlElement
      public void setName(String name) {
          this.name = name;
      }
  
      public int getAge() {
          return age;
      }
  
      @XmlElement
      public void setAge(int age) {
          this.age = age;
      }
  
      public int getId() {
          return id;
      }
  
      @XmlAttribute
      public void setId(int id) {
          this.id = id;
      }
  
  }
  </code></pre>