ESRI

I've been recently writing some ESRI desktop extension code in Java, and here are some things I've had to figure out the hard way. ESRI's support for the Java api is pretty awful. Even in the java docs, example code is written in Visual Basic?! WTF. And some of the examples given in Java on the EDN don't even work. One such example is creating an InMemoryWorkspace.

Create an in memory workspace in java

This is how you must do it. You must use Proxy classes, if you attempt to cast like the EDN examples would have you do, you will get ClassCastException. I found this technique on a forum posting by Christopher Moore. Thanks Dude!

		IWorkspaceFactory workspaceFactory = new InMemoryWorkspaceFactory();
		IWorkspaceName workspaceName = workspaceFactory.create("", "MyWorkspace", null, 0);
 
		IName name = (IName) workspaceName;
		IWorkspace workspace = new IWorkspaceProxy(name.open());
		IFeatureWorkspace featureWorkspace = new IFeatureWorkspaceProxy(workspace);

Create a table, add data to it, and create a recordset from the table

  Fields fields = new Fields();
 
  // add as many fields as needed.  esri will add an additional field called ObjectId at the end.
  Field field = new Field();
  field.setName("myField");
  field.setLength(20);
  field.setType(esriFieldType.esriFieldTypeString);
  fields.addField(field);
 
  (ITable) tbl = featureWorkspace.createTable("myTable", fields, null, null, null);
 
  IRow row = tbl.createRow();
  row.setValue(0, "data"); // first parameter is the index of the field (the order you created them in)
  row.store();
 
  IRecordSetInit rs = new RecordSet();
  rs.setSourceTable(tbl, null);
  IRecordSet recordSet = (IRecordSet)rs;