主要内容

Set Up Java Development Environment

To integrate MATLAB® code into Java® applications, start by authoring your MATLAB code using the MATLAB desktop. Then, use MATLAB Compiler SDK™ to generate Java packages. Finally, complete the integration process in your preferred Java development environment, such as a text editor with the OpenJDK® command line tools or an integrated development environment of your choice.

Install JDK or JRE

To develop Java applications, you must install the proper version of the Java Developer's Kit (JDK).

If you are not packaging MATLAB code or developing Java applications, you can install the Java Runtime Environment (JRE™) instead of the JDK™ to run Java applications.

MATLAB supports the use of OpenJDK. For supported versions, see Versions of OpenJDK Compatible with MATLAB by Release.

On Windows®, you can automatically set the JAVA_HOME environment variable during installation by selecting the Set JAVA_HOME variable option on the Custom Setup screen.

Verify your Java installation by typing the following at the system terminal:

java -version

Set JAVA_HOME Environment Variable

After you install the JDK or the JRE, set the system environment variable JAVA_HOME to your Java installation folder if you have not already done so during installation.

  1. Use the following table to set JAVA_HOME permanently according to your operating system.

    Operating SystemProcedure
    Windows
    1. Run C:\Windows\System32\SystemPropertiesAdvanced.exe and click the Environment Variables... button.

    2. Select the system variable JAVA_HOME and click Edit....

      If you do not have administrator rights on the machine, select the user variable JAVA_HOME instead of the system variable.

    3. Click New and add the path to your Java installation folder. For example, C:\Program Files\AdoptOpenJDK\jdk-8.0.282.8-hotspot.

    4. Click OK to apply the change.

    Linux®

    In a Bash shell, enter the following commands:

    echo "export JAVA_HOME=<path_to_Java_install>" >> ~/.bash_profile
    source ~/.bash_profile

    macOS (Mojave 10.14 or Earlier)

    In a Bash shell, enter the following commands:

    echo "export JAVA_HOME=<path_to_Java_install>" >> ~/.profile
    source ~/.profile

    macOS (Catalina 10.15 or Later)

    In a Zsh shell, enter the following commands:

    echo "export JAVA_HOME=<path_to_Java_install>" >> ~/.zprofile
    source ~/.zprofile

  2. If you are packaging MATLAB code, verify that MATLAB reads the correct value of JAVA_HOME.

    At the MATLAB command prompt, type getenv("JAVA_HOME") to display the value of JAVA_HOME.

Configure Classpath for Java Compilation and Execution

To configure the classpath for Java compilation and execution, you need to know which data API you plan to use. There are two options: the modern MATLAB Data API for Java introduced in R2026a and the legacy MWArray API. The choice of which JAR file to include in your classpath depends on which of these two data APIs you intend to use in your application. For details, see Choose Java Deployment Option.

MATLAB Data API for Java

Add the following JAR file to your classpath during compilation and execution:

  • matlabruntime.jar

If MATLAB is installed on your system

C:\Program Files\MATLAB\R2026a\toolbox\javabuilder\jar\matlabruntime.jar

If MATLAB Runtime is installed on your system

C:\Program Files\MATLAB\MATLAB Runtime\R2026a\toolbox\javabuilder\jar\matlabruntime.jar

When using the MATLAB Data API for Java, a JAR file is not created at the end of the packaging process. Instead, a deployable archive (.ctf file) is created. This file does not need to be placed on the classpath. Instead, the Java application must programmatically specify its location by passing it as an argument to the com.mathworks.runtime.MatlabRuntime class.

For example, you can specify the classpath at compile time as follows:

javac -classpath "matlabroot/toolbox/javabuilder/jar/matlabruntime.jar" MyApp.java

For example, you can specify the classpath at runtime as follows:

java -classpath "matlabroot/toolbox/javabuilder/jar/matlabruntime.jar" MyApp
Replace matlabroot with the full path to your MATLAB installation directory.

MWArray API

Add the following JAR files to your classpath during compilation and execution:

  • javabuilder.jar

  • JAR file generated by MATLAB Compiler SDK

If MATLAB is installed on your system

C:\Program Files\MATLAB\R2026a\toolbox\javabuilder\jar\javabuilder.jar

If MATLAB Runtime is installed on your system

C:\Program Files\MATLAB\MATLAB Runtime\R2026a\toolbox\javabuilder\jar\javabuilder.jar

When using the MWArray API, MATLAB Compiler SDK packages the MATLAB code into a JAR file. This JAR file, along with javabuilder.jar, must be added to the classpath of the Java application. The Java application can then access the packaged MATLAB code by referencing the classes and methods provided in the JAR file. Unlike the MATLAB Data API for Java, there is no need to programmatically specify the location of a .ctf file, as all necessary Java classes are included in the JAR.

For example, you can specify the classpath at compile time as follows:

javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar;.\MyPackagedMatlabCode.jar" MyApp.java

For example, you can specify the classpath at runtime as follows:

java -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar;.\MyPackagedMatlabCode.jar" MyApp
Replace matlabroot with the full path to your MATLAB installation directory.

Configure Native Library Access for Runtime Libraries

To run Java applications that use components generated by MATLAB Compiler SDK, the Java Virtual Machine (JVM) must be able to locate the native libraries included in MATLAB or MATLAB Runtime. These are platform-specific files (.dll, .so, or .dylib) are required for loading and executing packaged MATLAB code.

To make these libraries available, update the operating system’s native library search path by modifying the appropriate environment variable.

PlatformEnvironment Variable
WindowsPATH
LinuxLD_LIBRARY_PATH
macOSDYLD_LIBRARY_PATH

For details, see Set MATLAB Library Paths for Testing Deployed Applications.

Caution

When testing applications on a machine with a full MATLAB installation, you can set up native library access using the MATLAB installation.

In a deployment scenario where MATLAB Runtime is used, you must explicitly specify the location of the native libraries within an installation of MATLAB Runtime. This distinction ensures that deployed applications run independently of a full MATLAB installation.

See Also

Topics