How to call Java from SQL Server

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2019)noAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

SQL Server Language Extensions uses the sp_execute_external_script system stored procedure as the interface to call the Java runtime.

This how-to article explains implementation details for Java classes and methods that execute on SQL Server.

Where to place Java classes

There are two methods for calling Java classes in SQL Server:

  1. Place .class or .jar files in your Java classpath.

  2. Upload compiled classes in a .jar file and other dependencies into the database using the external library DDL.

Note

As a general recommendation, use .jar files and not individual .class files. This is common practice in Java and will make the overall experience easier. See also, How to create a jar file from class files.

Use Classpath

Basic principles

The following are some basic principles when executing Java on SQL Server.

  • Compiled custom Java classes must exist in .class files or .jar files in your Java classpath. The CLASSPATH parameter provides the path to the compiled Java files.

  • The Java method you are calling must be provided in the script parameter on the stored procedure.

  • If the class belongs to a package, the packageName must be provided.

  • params is used to pass parameters to a Java class. Calling a method that requires arguments is not supported. Therefore, parameters the only way to pass argument values to your method.

Note

This note restates supported and unsupported operations specific to Java in CTP 3.0.

  • On the stored procedure, input parameters are supported. Output parameters are not.
  • Streaming using the sp_execute_external_script parameter @r_rowsPerRead is not supported.
  • Partitioning using @input_data_1_partition_by_columns is not supported.
  • Parallel processing using @parallel=1 is supported.

Call Java class

The sp_execute_external_script system stored procedure is the interface used to call the Java runtime. The following example shows an sp_execute_external_script using the Java extension, and parameters for specifying path, script, and your custom code.

Note

Note that you don't need to define which method to call. By default, a method called execute is called. This means that you need to follow the Extensibility SDK for Java in SQL Server and implement an execute method in your Java class.

DECLARE @param1 int
SET @param1 = 3

EXEC sp_execute_external_script
  @language = N'Java'
, @script = N'<packageName>.<ClassName>'
, @input_data_1 = N'<Input Query>'
, @param1 = @param1

Set CLASSPATH

Once you have compiled your Java class or classes and created a jar file in your Java classpath, you have two options for providing the classpath to the SQL Server Java extension:

  1. Use external libraries

    The easiest option is to make SQL Server automatically find your classes by creating external libraries and pointing the library to a jar. Use external libraries for Java

  2. Register a system environment variable

    You can create a system environment variable and provide the paths to your jar file that contains the classes. Create a system environment variable called CLASSPATH.

Use external library

In SQL Server 2019 CTP 3.0, you can use external libraries for the Java language on Windows and Linux. You can compile your classes into a .jar file and upload the .jar file and other dependencies into the database using the CREATE EXTERNAL LIBRARY DDL.

Example of how to upload a .jar file with external library:

CREATE EXTERNAL LIBRARY myJar
FROM (CONTENT = '<local path to .jar file>') 
WITH (LANGUAGE = 'Java'); 
GO

By creating an external library, SQL Server will automatically have access to the Java classes and you do not need to set any special permissions to the classpath.

Example of calling a method in a class from a package uploaded as an external library:

EXEC sp_execute_external_script
  @language = N'Java'
, @script = N'MyPackage.MyCLass'
, @input_data_1 = N'SELECT * FROM MYTABLE'
with result sets ((column1 int))

For more information, see CREATE EXTERNAL LIBRARY.

Next steps