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 JDK™ 7 or 8.

  • 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. <arch> is your computer architecture. For example, win64 for 64–bit Microsoft® Windows® machines, maci64 on macOS, or glnxa64 on Linux®.

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

This table lists the names of the environment variables and the values of the paths.

Operating SystemVariablePath

Windows

PATH

matlabroot\bin\win64

64-bit Apple Mac

DYLD_LIBRARY_PATH

matlabroot/bin/maci64

64-bit Linux

LD_LIBRARY_PATH

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

Compile and Run Java Code on Windows

Compile your Java code:

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

Run the Java program:

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

MATLAB engine API for Java supports only maci64 on macOS systems.

Compile the Java code:

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

Specify Java Library Path and Run Program

Specify the Java library path and run Java program in one statement.

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

Set System Variable and Run Program

Set the DYLD_LIBRARY_PATH variable and run Java program. For example, using a C shell:

setenv DYLD_LIBRARY_PATH matlabroot/bin/maci64:$DYLD_LIBRARY_PATH
java -classpath .:matlabroot/extern/engines/java/jar/engine.jar MyJavaCode

Set Variables from C Shell

You can put these commands in a startup script, such as ~/.cshrc.

setenv DYLD_LIBRARY_PATH matlabroot/bin/maci64:$DYLD_LIBRARY_PATH

Set Variables in Bourne Shell

You can put these commands in a startup script such as ~/.profile.

DYLD_LIBRARY_PATH=matlabroot/bin/maci64:$DYLD_LIBRARY_PATH
export DYLD_LIBRARY_PATH 

Using Early Builds of Java Version 1.8.0

When using early builds of Java version 1.8.0, such as 1.8.0_111, the DYLD_LIBRARY_PATH environment variable might not be recognized. If you receive a java.lang.UnsatisfiedLinkError exception, set the java.library.path explicitly:

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.

Compile Java code:

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.

Specify the Java library path and run the Java program in one statement.

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

Set System Variable and Run Program

Set the LD_LIBRARY_PATH variable and run Java program. For example, using a C shell:

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

Set Variables from C Shell

You can put these commands in a startup script, such as ~/.cshrc.

setenv LD_LIBRARY_PATH matlabroot/bin/glnxa64:matlabroot/sys/os/glnxa64:$LD_LIBRARY_PATH

Set Variables from Bourne Shell

You can put these commands in a startup script such as ~/.profile.

LD_LIBRARY_PATH=matlabroot/bin/glnxa64:matlabroot/sys/os/glnxa64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH 

Related Topics