Main Content

Build Java Engine Programs

General Requirements

To set up your Java® environment for building engine applications:

  • Add matlabroot/extern/engines/java/jar/engine.jar to your Java class path.

  • Build the engine application with a supported version of JDK™. For version information, see MATLAB Interfaces to Other Languages.

  • Ensure your JRE™ is not an earlier version than your JDK.

To run Java, add the folder matlabroot/bin/arch to your system environment variable. Use these values to determine the variable name and the path:

  • Variable — the name of the environment variable.

  • Path — the value to assign to Variable.

  • matlabroot — the value returned by the MATLAB® matlabroot command. This command returns the folder where MATLAB is installed.

  • arch — the value returned by the computer("arch") command.

Operating SystemVariablePath

Windows®

PATH

matlabroot\extern\bin\win64

macOS with Apple silicon

DYLD_LIBRARY_PATH

matlabroot/extern/bin/maca64

macOS with Intel®

DYLD_LIBRARY_PATH

matlabroot/extern/bin/maci64

Linux®

LD_LIBRARY_PATH

matlabroot/extern/bin/glnxa64:matlabroot/sys/os/glnxa64

Compile and Run Java Code on Windows

Compile your Java code MyJavaCode.java:

javac -classpath matlabroot\extern\engines\java\jar\engine.jar MyJavaCode.java

Run the Java program MyJavaCode:

java -classpath .;matlabroot\extern\engines\java\jar\engine.jar MyJavaCode

Set System Path

To set the runtime library path from the Windows command prompt, type the following command.

set PATH=matlabroot\bin\win64;%PATH%

Set the path every time you open the Windows Command Processor.

You can also set the PATH variable from the System Properties dialog box. From the Control Panel > System > Advanced system settings > Advanced tab, click Environment Variables. Under System variables, select Path and click Edit. Modify Path by inserting matlabroot\bin\win64; at the beginning of the Variable Value. Click OK to close the dialog boxes, then close the Control Panel dialog box.

Compile and Run Java Code on macOS

Compile the Java code MyJavaCode.java:

javac -classpath matlabroot/extern/engines/java/jar/engine.jar MyJavaCode.java

Specify Java Library Path and Run Program

To specify the Java library path and run the Java program MyJavaCode on macOS with Apple silicon, type:

java -Djava.library.path=matlabroot/bin/maca64 -classpath .:matlabroot/extern/engines/java/jar/engine.jar MyJavaCode

On macOS with Intel, type:

java -Djava.library.path=matlabroot/bin/maci64 -classpath .:matlabroot/extern/engines/java/jar/engine.jar MyJavaCode

Compile and Run Java Code on Linux

MATLAB engine API for Java supports only glnxa64 on Linux systems.

To compile Java code MyJavaCode.java, type:

javac -classpath matlabroot/extern/engines/java/jar/engine.jar MyJavaCode.java

Specify Java Library Path and Run Program

If a compatible GCC library is in the search path, you can add matlabroot/bin/glnxa64 to the Java library search path and run the examples without setting the LD_LIBRARY_PATH variable. For information on supported compilers, see Supported and Compatible Compilers.

To specify the Java library path and run the Java program MyJavaCode, type:

java -Djava.library.path=matlabroot/bin/glnxa64 -classpath .:matlabroot/extern/engines/java/jar/engine.jar MyJavaCode

Set System Variable and Run Program

To set the LD_LIBRARY_PATH variable and run Java program, type:

LD_LIBRARY_PATH = matlabroot/bin/glnxa64:matlabroot/sys/os/glnxa64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH
java -classpath .:matlabroot/extern/engines/java/jar/engine.jar matlabroot

Related Topics