Main Content

Configure Your Environment for Generating Java Packages

Before you can generate Java® packages using MATLAB® Compiler SDK™ or run Java applications, ensure that your Java environment is properly configured. You must verify that:

  • Your development system uses a version of the Java Developer’s Kit (JDK™) that is compatible with MATLAB. For updated Java system requirements, see MATLAB Interfaces to Other Languages.

  • The JAVA_HOME environment variable is set to the folder containing your Java installation.

  • Your classpath contains the MATLAB javabuilder library JAR file and the JAR files for the packages containing your compiled MATLAB code. You can specify a classpath either in the javac command or in the CLASSPATH system environment variable.

  • Your target machine has MATLAB or MATLAB Runtime installed. For instructions on how to install MATLAB Runtime, see Download and Install MATLAB Runtime.

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 compiling MATLAB code or developing Java applications, you can install the Java Runtime Environment (JRE™) instead of the JDK to run Java applications.

  1. Verify the version of Java your MATLAB installation is using by running the following MATLAB command:

    version -java
  2. Download and install the JDK with the same major version from https://adoptium.net/. For example, if version -java returns Java 1.8.X, install OpenJDK 8.

    In 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.

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 compiling 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.

Set CLASSPATH

To build and run a Java application that uses a component generated by MATLAB Compiler SDK, the class path must include:

  • Classes in the com.mathworks.toolbox.javabuilder package, which is located in matlabroot/toolbox/javabuilder/jar/javabuilder.jar, where matlabroot represents your MATLAB or MATLAB Runtime installation folder.

  • Java packages that you have developed.

When you compile a Java application, you must specify a classpath either in the javac command or in the CLASSPATH system environment variable. Similarly, when you deploy a Java application, the end user must specify a classpath either in the java command or in the CLASSPATH system environment variable. For an example on setting the class path, see Compile and Run MATLAB Generated Java Application.

Set Shared Library Path Variable

Add the bin subfolder of your Java installation to your shared library path environment variable.

Use the following table to set the library path according to your operating system.

Operating SystemProcedure
Windows

The OpenJDK installer for Windows automatically sets the library path during installation. If you do not use the installer, complete the following steps to set the PATH environment variable permanently.

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

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

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

  3. Click New and add the path to the folder <path_to_Java_install>\bin.

  4. Click OK to apply the change.

Linux

In a Bash shell, enter the following commands:

echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<path_to_Java_install>/bin" >> ~/.bash_profile
source ~/.bash_profile

macOS (Mojave 10.14 or Earlier)

In a Bash shell, enter the following commands:

echo "export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:<path_to_Java_install>/bin"\
 >> ~/.profile
source ~/.bash_profile

macOS (Catalina 10.15 or Later)

In a Zsh shell, enter the following commands:

echo "export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:<path_to_Java_install>/bin"\
 >> ~/.zprofile
source ~/.zprofile

Note

In order to run Java applications that contain compiled MATLAB code, you must include the MATLAB or MATLAB Runtime library folders in your system library path. For details, see Set MATLAB Runtime Path for Deployment.

Related Topics