title: Java classes to c structs
body: <p>I have two sources:</p>
  
  <ul>
  <li>an xml-file containing definitions of <strong>several hundred</strong> data structures, basically structs with fields consisting of simple data types (e.g. <code>int</code>, <code>short</code>, <code>boolean</code>, <code>enum</code>), and</li>
  <li>a c header-file containing the same data structures represented a c structs. (I cannot modify this header-file or the c structs.)</li>
  </ul>
  
  <p>I also have a library, written in c, that is able to use these c structs to perform a specific task.</p>
  
  <p>Last I have a Java application which needs to use this library somehow.</p>
  
  <p>Currently I generate Java classes from the xml-file representing the data structures, now I <em>"just"</em> need to provide them to the library somehow.</p>
  
  <p>My current approach is to generate jni code, to call a c-function for each struct from Java, accepting the Java generated class as a parameter. Then generate c-code which takes the Java classes and fill in the appropriate fields in the c-structs.</p>
  
  <p><strong>Is this a good approach? Does anybody have suggestions for a simpler approach?</strong></p>
  
  <p>I am considering using sockets and e.g. <a href="http://code.google.com/p/protobuf/" rel="nofollow">protobuf</a>, but I do not see this as any simpler.</p>
  
  <p>Thanks in advance for any suggestions.</p>