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_HOMEenvironment variable is set to the folder containing your Java installation.Your
classpathcontains the MATLABjavabuilderlibrary JAR file and the JAR files for the packages containing your compiled MATLAB code. You can specify aclasspatheither in thejavaccommand or in theCLASSPATHsystem 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.
Verify the version of Java your MATLAB installation is using by running the following MATLAB command:
version -java
Download and install the JDK with the same major version from https://adoptium.net/. For example, if
version -javareturnsJava 1.8., install OpenJDK 8.XIn Windows®, you can automatically set the
JAVA_HOMEenvironment 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.
Use the following table to set
JAVA_HOMEpermanently according to your operating system.Operating System Procedure Windows Run
C:\Windows\System32\SystemPropertiesAdvanced.exeand click the Environment Variables... button.Select the system variable
JAVA_HOMEand click Edit....If you do not have administrator rights on the machine, select the user variable
JAVA_HOMEinstead of the system variable.Click New and add the path to your Java installation folder. For example,
C:\Program Files\AdoptOpenJDK\jdk-8.0.282.8-hotspot.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
If you are compiling MATLAB code, verify that MATLAB reads the correct value of
JAVA_HOME.At the MATLAB command prompt, type
getenv JAVA_HOMEto display the value ofJAVA_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.javabuilderpackage, which is located in, wherematlabroot/toolbox/javabuilder/jar/javabuilder.jarmatlabrootrepresents 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 System | Procedure |
|---|---|
| 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
|
| 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.