Matlab detect Java
Matlab or GNU Octave “.m” script functionality can be readily extended with Java code within the “.m” file. Matlab or GNU Octave do not necessarily include Java from the factory installation. Even if a compatible JDK is installed in Matlab or GNU Octave, Java can be disabled by -nojvm.
Detect if JVM is available from within a Matlab script by:
ok = usejava('jvm');Obtain the Java version:
javaMethod('getProperty', 'java.lang.System', 'java.version')GNU Octave Java
GNU Octave also has a JVM interface that extends Octave functionality.
The same commands as for Matlab generally work in Octave via builtin functions javaMethod() and javaObject().
If Java isn’t available in Octave, see if no location or an out of date location to Java is set in the environment variable JAVA_HOME:
getenv('JAVA_HOME')Currently GNU Octave doesn’t have a way to disable Java from the command line like Matlab’s -nojvm option. One might try setting environment variable JAVA_HOME to an invalid location before starting Octave to simulate no JVM.
macOS
On macOS with Homebrew, Java for Octave can be installed and configured like:
brew install openjdkDetermine the location of the Java Virtual Machine library “libjvm.dylib”:
gfind -L $(brew --prefix openjdk) -name libjvm.dylibAssuming the path is like “/opt/homebrew/opt/openjdk/libexec/openjdk.jdk/Contents/Home/lib/server/libjvm.dylib”, add to file “~/.octaverc”:
setenv('JAVA_HOME', '/opt/homebrew/opt/openjdk/libexec/openjdk.jdk/Contents/Home/lib/server/')