title: Sending iDOC from Java to SAP
body: <p>We have iDOC files that are generated from our system. Now we are trying to send them to SAP via RFC connection. I already etablished an RFC connection <strong>AS A CLIENT</strong> but i am unable to send iDOC!</p>
  
  <p>I tried to create a sample iDOC to test it but it doesn't work!</p>
  
  <pre><code>Exception in thread "main" java.lang.NoSuchMethodError: com.sap.conn.idoc.jco.SAPRepository$ConnectionProvider.execute(Lcom/sap/conn/jco/JCoFunction;Lcom/sap/conn/jco/rt/AbapRepository;)V
      at com.sap.conn.idoc.jco.SAPRepository.queryRootSegmentMetaData40(SAPRepository.java:1204)
      at com.sap.conn.idoc.jco.SAPRepository.queryRootSegmentMetaData(SAPRepository.java:1110)
      at com.sap.conn.idoc.jco.SAPRepository.getRootSegmentMetaData(SAPRepository.java:909)
      at com.sap.conn.idoc.rt.DefaultIDocDocument.&lt;init&gt;(DefaultIDocDocument.java:124)
      at com.sap.conn.idoc.rt.DefaultIDocDocument.&lt;init&gt;(DefaultIDocDocument.java:61)
      at com.sap.conn.idoc.jco.JCoIDocDocument.&lt;init&gt;(JCoIDocDocument.java:51)
      at com.sap.conn.idoc.jco.JCoIDocRuntime.createIDocDocument(JCoIDocRuntime.java:133)
      at com.sap.conn.idoc.jco.JCoIDocRuntime.createIDocDocument(JCoIDocRuntime.java:35)
      at com.sap.conn.idoc.rt.DefaultIDocRuntime.createIDocDocument(DefaultIDocRuntime.java:175)
      at com.sap.conn.idoc.rt.DefaultIDocRuntime.createIDocDocument(DefaultIDocRuntime.java:18)
      at com.idoc.sender.IDocClientExample.main(IDocClientExample.java:49)
  </code></pre>
  
  <p>I even tried to send a sample iDOC XML but i am also facing another error, tried to change the XML many times but i am facing the same problem again an again!</p>
  
  <pre><code>com.sap.conn.idoc.IDocParseException: (7) IDOC_ERROR_PARSE_FAILURE: Invalid character encountered in XML input data source:
  state=READING_XMLDECLARATION, charPosition=14, lineNumber=1, columnNumber=15, invalidChar=U+003D,
  sourceSnippet=...@&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;DEBMAS06&gt;&lt;IDOC&gt;&lt;EDI_DC40&gt;&lt;DOCNUM&gt;20120114120000&lt;/DOCNUM&gt;&lt;IDO...
                                 ^
      at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.throwIDocParseException(DefaultIDocXMLParser.java:2223)
      at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.parseProlog(DefaultIDocXMLParser.java:1635)
      at com.sap.conn.idoc.rt.xml.DefaultIDocXMLParser.parse(DefaultIDocXMLParser.java:320)
      at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:112)
      at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:54)
      at com.sap.conn.idoc.rt.xml.DefaultIDocXMLProcessor.parse(DefaultIDocXMLProcessor.java:31)
      at com.idoc.sender.IDocClientExample.main(IDocClientExample.java:57)
  </code></pre>
  
  <p>This is the code i am using to create the iDOC and send the iDOC XML:</p>
  
  <pre><code>package com.idoc.sender;
  
  
  import com.sap.conn.idoc.*;
  import com.sap.conn.idoc.jco.JCoIDoc;
  import com.sap.conn.jco.JCoDestination;
  import com.sap.conn.jco.JCoDestinationManager;
  
  import java.io.BufferedReader;
  import java.io.FileReader;
  
  
  public class IDocClientExample {
  
   public static void main(String[] args) {
  
    try {
  
     String iDocXML = null;
     FileReader fileReader;
  
     try {
      fileReader = new FileReader("TestSalesOrder.xml");
      BufferedReader br = new BufferedReader(fileReader);
      StringBuffer sb = new StringBuffer();
      String line;
      while ((line = br.readLine()) != null) {
       sb.append(line);
      }
  
      iDocXML = sb.toString();
  
      br.close();
      fileReader.close();
     } catch (Exception ex) {
      ex.printStackTrace();
     }
  
     // see provided configuration file xxxx.jcoDestination
     JCoDestination destination = JCoDestinationManager.getDestination("xxxx");
     IDocRepository iDocRepository = JCoIDoc.getIDocRepository(destination);
     String tid = destination.createTID();
     IDocFactory iDocFactory = JCoIDoc.getIDocFactory();
  
     System.out.println(destination.getAttributes());
  
  
     // a) create sample new idoc
     IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "SYSTAT01");
     IDocSegment segment = doc.getRootSegment();
     segment.addChild("E1MARAM");
     JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
  
  
     // b) use existent xml file
     IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
     IDocDocumentList iDocList = processor.parse(iDocRepository, iDocXML);
     JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
     destination.confirmTID(tid);
  
    } catch (Exception e) {
     e.printStackTrace();
    }
    System.out.print("program end");
   }
  }
  // a) create sample new idoc
  IDocDocument doc = iDocFactory.createIDocDocument(iDocRepository, "SYSTAT01");
  IDocSegment segment = doc.getRootSegment();
  segment.addChild("E1MARAM");
  JCoIDoc.send(doc, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
  
  
  // b) use existent xml file
  IDocXMLProcessor processor = iDocFactory.getIDocXMLProcessor();
  IDocDocumentList iDocList = processor.parse(iDocRepository, iDocXML);
  JCoIDoc.send(iDocList, IDocFactory.IDOC_VERSION_DEFAULT, destination, tid);
  destination.confirmTID(tid);
  
  } catch (Exception e) {
   e.printStackTrace();
  }
  System.out.print("program end");
  }
  }
  </code></pre>
  
  <p>This is my connection file:</p>
  
  <pre><code>jco.client.lang=EN
  jco.client.client=100
  jco.client.passwd=xxxx
  jco.client.user=xxxx
  jco.client.sysnr=01
  jco.client.ashost=xxxx
  </code></pre>
  
  <p>This is my xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;DEBMAS06&gt;
  &lt;IDOC&gt;
  &lt;EDI_DC40&gt;
  &lt;DOCNUM&gt;20120114120000&lt;/DOCNUM&gt;&lt;IDOCTYP&gt;DEBMAS06&lt;/IDOCTYP&gt;
  &lt;MESTYP&gt;DEBMAS&lt;/MESTYP&gt;
  &lt;SNDPOR&gt;HTTPDEMO&lt;/SNDPOR&gt;
  &lt;SNDPRT&gt;LS&lt;/SNDPRT&gt;
  &lt;SNDPRN&gt;HTTPDEMO&lt;/SNDPRN&gt;
  &lt;RCVPOR&gt;SAPxxx&lt;/RCVPOR&gt;
  &lt;RCVPRT&gt;LS&lt;/RCVPRT&gt;
  &lt;RCVPRN&gt;xxxCLNT100&lt;/RCVPRN&gt;
  &lt;/EDI_DC40&gt;
  &lt;E1KNA1M segment="1"&gt;
  &lt;KUNNR&gt;47&lt;/KUNNR&gt;
  &lt;NAME1&gt;Test Customer&lt;/NAME1&gt;
  &lt;KTOKD&gt;0004&lt;/KTOKD&gt;
  &lt;SORTL&gt;TEST&lt;/SORTL&gt;
  &lt;ORT01&gt;City&lt;/ORT01&gt;
  &lt;LAND1&gt;PL&lt;/LAND1&gt;
  &lt;LZONE&gt;A&lt;/LZONE&gt;
  &lt;SPRAS&gt;L&lt;/SPRAS&gt;
  &lt;STRAS&gt;Street&lt;/STRAS&gt;
  &lt;/E1KNA1M&gt;
  &lt;/IDOC&gt;
  &lt;/DEBMAS06&gt;
  </code></pre>
  
  <p>I am using sapjco3-x86-64bit-3.0.5 and sapidoc-3.0.1!</p>
  
  <p>Testing the creation of iDOC and sending IDOC XML is just for test but in our case, we have the iDOC ready!</p>
  
  <p>My question is, how can i send iDOC <strong>directly from my disk</strong> to SAP via RFC? If i can't do so, how can i slove the error of creating iDOC or sending iDOC XML? My second question, once i am able to send iDOC, do i have to etablish a new SERVER RFC connection so i can receive iDOC from SAP?</p>
  
  <p>** <strong><em>EDIT</em></strong> **</p>
  
  <p>Now i ma able to send and receive IDoc properly. My problem is how i can get the IDoc as a <strong>Flat IDoc</strong>.</p>
  
  <p>I can receive my IDoc as <strong>XML IDoc</strong> with this code:</p>
  
  <pre><code>package com.idoc.actif;
  
  import com.sap.conn.idoc.IDocDocumentList;
  import com.sap.conn.idoc.IDocXMLProcessor;
  import com.sap.conn.idoc.jco.*;
  import com.sap.conn.jco.server.*;
  
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.OutputStreamWriter;
  
  
  public class IDocServerExample
  {
      public static void main(String[] a)
      {   
          try
          {
              // see provided examples of configuration files xxxx.jcoServer and  xxxx.jcoDestination
              JCoIDocServer server = JCoIDoc.getServer("xxxx");
              server.setIDocHandlerFactory(new MyIDocHandlerFactory());
  
              server.setTIDHandler(new MyTidHandler());
  
              MyThrowableListener listener = new MyThrowableListener();
              server.addServerErrorListener(listener);
              server.addServerExceptionListener(listener);
              //server.setConnectionCount(1);
              server.start();
  
          }
          catch (Exception e) 
          {
              e.printStackTrace();
          }
       }
  
      static class MyIDocHandler implements JCoIDocHandler
      {
          public void handleRequest(JCoServerContext serverCtx, IDocDocumentList idocList)
          {
  
              FileOutputStream fos=null;
              OutputStreamWriter osw=null;
              try
              {
                  //receiving XML IDoc, how to get the IDoc as a Flat IDoc (SAP is sending the IDoc as Flat)???
                  IDocXMLProcessor xmlProcessor = JCoIDoc.getIDocFactory().getIDocXMLProcessor();
                  fos=new FileOutputStream(serverCtx.getTID());
                  osw=new OutputStreamWriter(fos, "UTF8");
                  xmlProcessor.render(idocList, osw, IDocXMLProcessor.RENDER_WITH_TABS_AND_CRLF);
                  osw.flush();
              }
              catch (Throwable thr)
              {
                  thr.printStackTrace();
              }
              finally
              {
                  try
                  {
                      if (osw!=null)
                          osw.close();
                      if (fos!=null)
                          fos.close();
                  }
                  catch (IOException e)
                  {
                      e.printStackTrace();
                  }
              }
          }
      }
  
      static class MyIDocHandlerFactory implements JCoIDocHandlerFactory
      {
          private JCoIDocHandler handler = new MyIDocHandler();
          public JCoIDocHandler getIDocHandler(JCoIDocServerContext serverCtx)
          {
              return handler;
          }
      }
  
      static class MyThrowableListener implements JCoServerErrorListener, JCoServerExceptionListener
      {
  
          public void serverErrorOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Error error)
          {
              System.out.println("&gt;&gt;&gt; Error occured on " + server.getProgramID() + " connection " + connectionId);
              error.printStackTrace();
          }
          public void serverExceptionOccurred(JCoServer server, String connectionId, JCoServerContextInfo ctx, Exception error)
          {
              System.out.println("&gt;&gt;&gt; Error occured on " + server.getProgramID() + " connection " + connectionId);
              error.printStackTrace();
          }
      }
  
      static class MyTidHandler implements JCoServerTIDHandler
      {
          public boolean checkTID(JCoServerContext serverCtx, String tid)
          {
              System.out.println("checkTID called for TID="+tid);
              return true;
          }
  
  
          public void confirmTID(JCoServerContext serverCtx, String tid)
          {
              System.out.println("confirmTID called for TID="+tid);
          }
  
  
          public void commit(JCoServerContext serverCtx, String tid)
          {
              System.out.println("commit called for TID="+tid);
          }
  
  
          public void rollback(JCoServerContext serverCtx, String tid)
          {
              System.out.print("rollback called for TID="+tid);
          }
      }
  }
  </code></pre>