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:
See Java plugins for complete details.
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:
file HelloFromJava.java | ||
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.
For this step, you may want to copy sfi-api.jar from your Stata intallation to your current directory.
This produces a new file, HelloFromJava.class:
% javac -classpath sfi-api.jar HelloFromJava.java
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.
Learn more about Stata's programming features.
To read more, see [P] java, [P] javacall, and visit stata.com/java.