»  Home »  Products »  Features »  Improvements for Java plugins
ORDER STATA

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 import fred command.

Stata provides a custom class loader with several benefits:

  • Stata does not need to be restarted after installing a Java plugin. This is particularly important for those distributing plugins to other users.
  • Dependencies can 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.
  • 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.

See Java plugins for complete details.

Highlights

  • Call Java methods
  • Leverage Java's extensive features
  • Access third-party Java libraries or write your own code
  • Custom class loader
  • Read and write Stata's stored 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
  • Methods in class Matrix make accessing Stata matrix elements easier
  • Methods in class Mata make accessing Mata matrix elements easier

Let's see it work

We will 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 installation 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 16 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 intro, [P] javacall, and visit stata.com/java.


Stata

Shop

Support

Company


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