Tips for Java in Stata

With the new Stata 13 comes the possibility to execute Java classes within Stata. Java classes are called with the command javacall and can interact with Stata datasets and macros.

This new feature is particularly useful to:
  • improve the performance of functions that would require a large use of macros, scalars and varlists if implemented in a Stata program;
  • directly load datasets from SQL servers, with possibility of manipulating queries in do-files;
  • use any kind of available Java library.

Here there are some tips to implement the usage of Java within Stata:
  • Java classes (.class) and JARs have to be copied to the personal ado files directory, for instance on Mac it is ~/Library/Application\ Support/Stata/ado/personal/, or ~/Library/Application\ Support/Stata/ado/personal/jar/ (if those directories do not exist they must be created);
  • classes and JARs are loaded by the JVM the first time javacall is called within a Stata session and they remain in memory (the JVM is not restarted by following calls of javacall), and this means that whenever a class is modified, Stata has to be relaunched;
  • if using an advanced editor for Java coding (e.g., Eclipse), it is useful to include stata-sfi.jar in the project to get syntax validation and other supporting features (on Mac it can be found at ~/Applications/Stata/ado/base/jar/);
  • the API is organized in a few packages and classes with static methods, so that, for instance, the Data class allows accessing and modifying the dataset, the Mata class using Mata, the Scalar class accessing and writing scalars, the SFIToolkit class displaying strings in the Stata Results window, etc.

1 comment:

  1. Thanks for the post, the Stata Java connection has proved useful to me when working with GIS data and performing more complicated tasks than just displaying.