Configure Your Environment for Generating Java Packages - MATLAB & Simulink (original) (raw)

Main Content

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:

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:
  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 System Procedure
    Windows Run C:\Windows\System32\SystemPropertiesAdvanced.exe and click the Environment Variables... button.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.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_profilesource ~/.bash_profile
    macOS (Mojave 10.14 or Earlier) In a Bash shell, enter the following commands: echo "export JAVA_HOME=<path_to_Java_install>" >> ~/.profilesource ~/.profile
    macOS (Catalina 10.15 or Later) In a Zsh shell, enter the following commands:echo "export JAVA_HOME=<path_to_Java_install>" >> ~/.zprofilesource ~/.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:

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 PATH environment variable permanently.Run C:\Windows\System32\SystemPropertiesAdvanced.exe and click the Environment Variables... button.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.Click New and add the path to the folder _<path_to_Java_install>_\bin.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_profilesource ~/.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"\ >> ~/.profilesource ~/.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"\ >> ~/.zprofilesource ~/.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.

See Also

Topics