Wrong Java version when calling via system()

7 views (last 30 days)
Hi,
I have java class program Main.class in folder F. When I do in terminal inside folder F
$java Main
the program works fine
When I call in Matlab
s = system(['cd ', F, ' && java ',Main]);
I get the error
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: Main : Unsupported major.minor version 52.0
This is strange, since I have Java version 51.0 installed in my MAC. Could it be that the Matlab system() call uses a different java version than that of the MAC OSX system?
Note: I've been running this command in a different machine, with an older Matlab version without problems.
Thank you for any pointers!

Accepted Answer

Walter Roberson
Walter Roberson on 20 Jul 2015
When you use system() you invoke your $SHELL with whatever environment variables MATLAB has set, including possible changes to your PATH, DYLD_FRAMEWORK_PATH, and DYLD_LIBRARY_PATH . In particular on OS-X, DYLD_LIBRARY_PATH will have several java component directories.
You can setenv() before your system() call, or you can use (depending on your shell)
system('setenv DYLD_LIBRARY_PATH ThePath...; real command here'); %or
system('set -x DYLD_LIBRARY_PATH=ThePath; real command here'); %or
system('DYLD_LIBRARY_PATH=ThePath real command here'); %notice no semi-colon after variable is set
  4 Comments
Walter Roberson
Walter Roberson on 12 Aug 2015
Ah yes. For those of you not familiar with Unix: the syntax
DYLD_LIBRARY_PATH=`/usr/libexec/java_home`
means to run the command /usr/libexec/java_home and take the output of the command and assign it to the shell variable DYLD_LIBRARY_PATH
In shells derived from the Bourne Shell "sh", such as ksh and bash, but not in shells derived from the C Shell "csh", if you prefix a command with setting an environment variable, with no semi-colon between the setting of the variable and the command to be executed, then that environment variable will be set to the given value only while that command is executed and will return to its previous value afterwards. For example,
HOME=/tmp printenv | grep HOME; printenv | grep HOME
the first output will show HOME=/tmp and the second output will show HOME=/Users/roberson (for me), without my having to store the old value, run the command, and restore the old value
Namshik Han
Namshik Han on 10 Mar 2016
Thanks all, yes, the 3rd one works for me too.
system('DYLD_LIBRARY_PATH=ThePath real command here'); %notice no semi-colon after variable is set

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!