Improvements for Java plugins

What's this about?

You can call Java plugins from Stata. Java plugins can interact with Stata's datasets, matrices, macros, scalars, and the like. You can use third-party libraries in your Java code. For example, you could write a Stata command in Java to pull data from the Federal Reserve Economic Data (FRED). It is the new import fred command.

In Stata 15, we have made it even easier for you to use the Stata-Java API.

The new custom class loader has several benefits:

  • Stata no longer needs to be restarted after installing a Java plugin. This is particularly important for those distributing plugins to other users.
  • Dependencies can now be isolated between plugins. This means one developer can use one version of a library and another developer can safely use another version of the same library in a different plugin.
  • During the development process, plugins can be unloaded by using the discard command. When the plugin is called again, you will get the version you just compiled. Previously, Stata needed to be restarted.

See Java plugins for complete details.


  • New custom class loader
  • Read and write Stata's return results
  • Read and write Stata's characteristics
  • Read and write Stata's string scalars
  • Read and write Stata's strL data type as a buffered array
  • Get the Stata data type of a variable in the current dataset
  • Create and write to Stata matrices
  • Call a Stata command from Java
  • New methods in class Matrix make accessing Stata matrix elements easier
  • New methods in class Mata make accessing Mata matrix elements easier

Let's see it work

Rather than simply telling you more about the improvements, let's demonstrate just how easy it is to add new features to Stata using Java.

We are going to create a new Stata command tryjava. Here it is in action:

. tryjava
  hello from java!

.    _

First, write a hello routine in Java and save it in a .java file:

import com.stata.sfi.*; public class HelloFromJava { public static int sayhello(String[] args) { SFIToolkit displayln("Hello from java!") ; return(0) ; // Stata return code } }

Compile the .java file using the Java compiler. This produces a new file, HelloFromJava.class:

% javac -classpath sfi-api.jar

Build a Java JAR file from this class file. You can name the JAR file whatever you wish. We will call it tryjava.jar:

% jar cvf tryjava.jar HelloFromJava.class

Copy the JAR file to your PERSONAL directory. (In Stata, type sysdir to find out where your PERSONAL directory is.)

Now write a Stata ado-file:

file tryjava.ado
program tryjava version 15 javacall HelloFromJava sayhello, jar(tryjava.jar) end
file tryjava.ado

That's it.

Tell me more

Learn more about Stata's programming features.

To read more, see [P] java, [P] javacall, and visit





The Stata Blog: Not Elsewhere Classified Find us on Facebook Follow us on Twitter LinkedIn Google+ YouTube
© Copyright 1996–2017 StataCorp LLC   •   Terms of use   •   Privacy   •   Contact us