- Undocumented Matlab - https://undocumentedmatlab.com -
Using Java 7 in Matlab R2013a and earlier
Posted By Yair Altman On June 19, 2013 | 15 Comments
Today I would like to introduce guest blogger Roderick, who wishes to remain anonymous. Roderick discusses his experience with setting up Matlab to use the latest stable release of Java, namely JVM 1.7, a.k.a. Java 7.
I work in the team where I do a lot of research and prototyping with Matlab. The final production code is however coded by a separate development team in Java 7. When running back-tests on our models I have to either ensure my Matlab prototype exactly matches the Java behavior, or to call the Java production code directly. However, although Java 7 has been available since July 2011 [1], the latest official Matlab version (R2013a at this point) still uses the JVM 1.6 (Java 6), which does not allow calling Java 7 code directly. Today’s article contains my experience in trying to let Matlab use JVM 1.7 code.
I urge readers to consider the following before trying the hacks I’ve found:
It is worth considering a number of alternatives before trying to setup Matlab with the JVM 1.7:
If after considering the alternatives you have a valid use-case for Matlab to call Java 7 code, follow these steps:
UseOldFileDialogs
featureThe text below details these steps.
To run Java 7 code from Matlab, Matlab needs to use JVM 1.7. To do this, see this MathWorks page [3], or follow these steps:
>> version -java
ans =
Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
MATLAB_JAVA
with the installation folder as its value. For example: “C:\Program Files\Java\jdk1.7.0_21\jre”
>> version('-java')
ans =
Java 1.7.0_21-b11 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
Certain Matlab functionality was found to give issues when using the JVM 1.7. Most problems are related to visibility and display, although most Matlab code ran as-is without issue.
Matlab hangs after clicking the <Import Data> button, or when trying to use the <Save As> functionality. This problem seems to be related to opening a certain type of window after which focus is not returned to Matlab. The user is forced to close Matlab via the Windows Task Manager. This problem was found in R2012b and earlier, however it seems fixed in R2013a. I have not found a solution for this problem; perhaps it is related to this issue [5]. Consider importing data via the command line, and first creating a new m-file then open it in the Matlab Editor. Alternatively, if you really get annoyed by this, you could use findobj functionality to disable the Matlab buttons.
Exceptions are occasionally thrown when multiple monitors are used, e.g. when moving figures from one screen to the other, or when docking figures. These exceptions can be a bit annoying, but they do not seem critical.
Matlab freezes when using uigetfile. It seems R2013a does not have this problem; for R2012b a solution was found by setting the undocumented UseOldFileDialogs
feature [6]:
feature('UseOldFileDialogs', 1)
Certain Matlab dialogs, such as questdlg, listdlg and inputdlg are missing some components. For example:
ButtonName = questdlg('What is your favorite color?', 'Color Question', 'Red', 'Green', 'Blue', 'Green');
set(0, 'DefaultFigureCreateFcn', @DefaultFigureCreationFcnForJVM7);
function DefaultFigureCreationFcnForJVM7(aObject, aEventdata)
% PURPOSE: This function acts as the default figure create function to enforce focus for UI dialog objects % when using JVM 7.
% IN: - aObject (1x1 float): the handle to figure object
% - aEventdata (1x1 struct): the event data
myDbStack = dbstack();
if length(myDbStack) > 1 && any(strcmp(myDbStack(2).file, 'hgloadStructDbl.m'))
% don't force visibility when GUIs are opened
else
set(aObject, 'Visible', 'on');
drawnow();
end
The UI dialogs flash a bit more when created, due to the fact that they resize and move around the screen for optimal placement. But at least they look ok now:
>> which listdlg
C:\Program Files\MATLAB\R2013a\toolbox\matlab\uitools\listdlg.m
fig_props = { ...
'name' figname ...
'color' get(0,'DefaultUicontrolBackgroundColor') ...
'resize' 'off' ...
'numbertitle' 'off' ...
'menubar' 'none' ...
'windowstyle' 'modal' ...
'visible' 'off' ...
... % comment out the createfcn to empty such that we don't override the specific createfcn for JVM 1.7!
... %'createfcn' '' ...
'position' fp ...
'closerequestfcn' 'delete(gcbf)' };
If you developed custom Matlab GUIs, you might notice visibility problems when opening the GUIs. For example, assume we have developed the following simple GUI:
myFigureHandle = TestGUI;
myChildHandles = get(myFigureHandle, 'Children')
set(myFigureHandle, 'Children', flipud(myChildHandles))
set(myChildHandles(1), 'Children', flipud(get(myChildHandles(1), 'Children')))
>> which gui_mainfcn
C:\Program Files\MATLAB\R2013a\toolbox\matlab\guide\gui_mainfcn.m
...
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
% Patched logic to ensure visibility of all objects
HandleJvm7SupportForGUIs(gui_hFigure, guidata(gui_hFigure));
...
function HandleJvm7SupportForGUIs(aGUIObject, aGUIHandles)
% PURPOSE: This function hacks around with the focus of objects on a GUI. It was found that GUIs do not
% work as intended with the jvm 7 setup, and that objects on panels lose focus/visibility.
% The approach which at is taken now is by flipping the order of childrens in uipanels.
% Before the order is swapped, the panel is set to be the visible top-layer panel.
% IN: - aGUIObject (1x1 float): the handle the GUI figure
% - aGUIHandles (1x1 struct): the GUI handles
if ~IsJvm7Used()
return
end
myListOfHandleArrayFields = 'tabPanels';
try
set(aGUIObject, 'Visible', 'on');
drawnow();
if isprop(aGUIObject, 'Children')
myChildren = get(aGUIObject, 'Children');
myIsMenuItem = false(length(myChildren), 1);
for i = 1:length(myChildren)
if isprop(myChildren(i), 'Type') && strcmp(get(myChildren(i), 'Type'), 'uimenu')
myIsMenuItem(i) = true();
end
end
myChildren(~myIsMenuItem) = flipud(myChildren(~myIsMenuItem));
set(aGUIObject, 'Children', myChildren);
drawnow();
end
myFields = fieldnames(aGUIHandles);
myIsValid = true(size(myFields));
for i = 1:length(myListOfHandleArrayFields)
myIsValid = myIsValid & cellfun('isempty', strfind(myFields, myListOfHandleArrayFields{i}));
end
myFields = myFields(myIsValid);
myPanelFields = {};
for i = 1:length(myFields)
if isprop(aGUIHandles.(myFields{i}), 'Type') && strcmp(get(aGUIHandles.(myFields{i}), 'Type'), 'uipanel')
myPanelFields = [myPanelFields; myFields(i)]; %#ok
end
end
for i = 1:length(myPanelFields)
% allow three layers of panels maximum!
if isprop(aGUIHandles.(myPanelFields{i}), 'Parent')
myParent = get(aGUIHandles.(myPanelFields{i}), 'Parent');
if strcmp(get(myParent, 'Type'), 'uipanel')
if isprop(myParent, 'Parent')
mySecondParent = get(myParent, 'Parent');
if strcmp(get(mySecondParent, 'Type'), 'uipanel')
uistack(mySecondParent, 'top');
drawnow();
end
end
uistack(myParent, 'top');
drawnow();
end
end
uistack(aGUIHandles.(myPanelFields{i}), 'top');
drawnow();
set(aGUIHandles.(myPanelFields{i}), 'Children', flipud(get(aGUIHandles.(myPanelFields{i}), 'Children')));
drawnow();
end
catch myException
disp(['WARN: could not initialize the GUI correctly with JVM 1.7 override logic, in ', mfilename()]);
myException.getReport()
end
function theIsJvm7Used = IsJvm7Used()
% PURPOSE: To indicate if Matlab is setup to use jvm 7
% OUT: - theIsJvm7Used (1x1 logical): whether or not JVM 7 is used
myJavaVersion = version('-java');
theIsJvm7Used = strcmpi(myJavaVersion(1:8), 'java 1.7');
Categories: Guest bloggers, GUI, High risk of breaking in future versions, Java
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/using-java-7-in-matlab-r2013a-and-earlier
URLs in this post:
[1] available since July 2011: http://en.wikipedia.org/wiki/Java_version_history
[2] Java RMI: http://docs.oracle.com/javase/tutorial/rmi/index.html
[3] this MathWorks page: http://www.mathworks.com/support/solutions/en/data/1-1812J/
[4] Oracle website: http://www.oracle.com/technetwork/java/javase/downloads/index.html
[5] this issue: http://undocumentedmatlab.com/blog/solving-a-matlab-hang-problem/
[6] UseOldFileDialogs
feature: http://undocumentedmatlab.com/blog/undocumented-feature-function/
[7] hidden children of the panel object: http://undocumentedmatlab.com/blog/panel-level-uicontrols/
[8] Using pure Java GUI in deployed Matlab apps : https://undocumentedmatlab.com/articles/using-pure-java-gui-in-deployed-matlab-apps
[9] Matlab-Java interface using a static control : https://undocumentedmatlab.com/articles/matlab-java-interface-using-static-control
[10] FindJObj – find a Matlab component's underlying Java object : https://undocumentedmatlab.com/articles/findjobj-find-underlying-java-object
[11] JMI – Java-to-Matlab Interface : https://undocumentedmatlab.com/articles/jmi-java-to-matlab-interface
[12] Matlab callbacks for Java events : https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events
[13] Matlab callbacks for Java events in R2014a : https://undocumentedmatlab.com/articles/matlab-callbacks-for-java-events-in-r2014a
[14] : https://undocumentedmatlab.com/blog/using-groovy-in-matlab/
[15] : https://undocumentedmatlab.com/blog/using-java-7-in-matlab-r2013a-and-earlier
[16] : https://undocumentedmatlab.com/blog/speeding-up-compiled-apps-startup
[17] : https://www.mathworks.com/matlabcentral/newsreader/view_thread/271589
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
15 Comments To "Using Java 7 in Matlab R2013a and earlier"
#1 Comment By Malcolm Lidierth On June 20, 2013 @ 04:12
Depending םn the Java features needed, another possibility is to stick to JVM 5/6 but use another JVM language to write the relevant code e.g. Groovy or Scala – see [14] for Groovy. Those implement some features made available in Java 7 e.g. strings in switch statements, but using JVM 5/6 compatible bytecode.
#2 Comment By Mike On July 3, 2013 @ 13:17
Looks like MATLAB R2013b (prerelease) have finally updated to Java 7
#3 Comment By kanmani On October 25, 2013 @ 21:19
i had completed my project in java using eclipse platform
can u help me to execute the same in matlab
very urgent
#4 Comment By julien On January 14, 2014 @ 12:41
Hello Yair,
I am working now with matlab R2013B and it appears that cursor does not update correctly on javacomponents… When I set cursors with jComponent.setCursor(aCursor), the cursor does not change when the mouse pointer hovers the component. It always appear as com.mathworks.hg.peer.FigureClientProxy$FigureDTClientBase’s cursor (controlled by the figures’s ‘pointer’ property), which is the first component for which cursor is set in the upwards JavaFrame hierarchy.
I suspect it has something to deal with the new lightweight implementation of hgjavacomponent’s container (great news!). But I am sure that you perfectly know this issue, that’s why I wondered if you had fixed this?
#5 Comment By Yair Altman On January 14, 2014 @ 12:53
@Julien – I confirm your observation but I’m sorry to disappoint you that I haven’t looked into this so far. I suspect that this is a Java thing, so I advice that you search the Java forums. Please get back here and report if you discover anything useful.
#6 Comment By julien On January 14, 2014 @ 13:32
Thanks for your fast answer, Yair. I already had a look on java forums and I did not find anything similar… For example, if you simply create a
JFrame
, add a component to the content pane and set its cursor from matlab command line, it works fine.I look forward to see your investigations!
#7 Comment By Martin Lechner On April 2, 2014 @ 02:41
I tried Matlab R2014a with Java JDK8. Normal plots and the GUI’s from standard matlab work correct.
#8 Pingback By Java class access pitfalls | Undocumented Matlab On July 2, 2014 @ 08:34
[…] if you do not have access to the source code, or if it uses some new JDK features), then consider installing a newer compatible JVM in Matlab.Note that when we try running the Java classes using the java executable (outside Matlab), we do […]
#9 Comment By Wayan Permana On July 2, 2014 @ 18:15
Hi yair,
Thanks for your hard work, it’s really help me a lot.
I have a problem with matlab r2013a ja builder, jar created by deploytool is working as expected in jdk 1.8 but figure is shown and dissapear again (figure contain no plot).
Then i change jdk to 1.6u17 (64 bit) to match matlab version but when i run the jar again but it give following error in netbeans :
Any suggestion, how to fix it? i really need the plot for presentation.
#10 Comment By Yair Altman On July 3, 2014 @ 00:24
@Wayan – it appears that some of your external Java classes (
SimultanBoxGraph
and possibly others) were compiled with JDK 1.7 (=51.0), and this does not work with 1.6. If you can, try recompiling everything using 1.6.Alternatively, try retrofitting your Matlab with JDK 1.7, as explained [15].
Alternatively, upgrade to Matlab R2013b (8.2) or newer, which uses 1.7 out-of-the-box.
Alternatively, try figuring out the problem with the disappearing figure in 1.8, using MathWorks support (then return here to let us know if you discover anything useful).
#11 Comment By Wayan Permana On July 3, 2014 @ 07:30
Thank you yair this actually solve my problem.
My bad, i was unaware deploytool use my default 1.7 jdk instead of jdk 1.6 that come from with MATLAB so i didnt recompile it that time.
This step works for me, set new JAVA_HOME variable to jdk 1.6 and recompile everything with deploytool.
Sorry yair, i have one more question about matlab ja builder, my m script run about 2-4 second in matlab but in java it run up to 2 minutes, is it normal? may i ask your suggestion to make it run faster in java?
I have a presentation about it, i dont want they think my program is slow because of inefficient algorithm.
Oh the script is simple, just about function optimization and draw plot of function.
#12 Comment By Yair Altman On July 3, 2014 @ 12:58
See [16]
#13 Comment By Fred On August 31, 2014 @ 15:56
I came here since I wanted to run an old version MATLAB on Win8. I wasn’t intending to test with the newer jre. But it could not find the default jre, and it turns out this is a msvcr71.dll problem. I just googled a copy of that dll and placed it in C:\Program Files (x86)\MATLAB\R2010a\bin\win32, and the problem is solved.
Source: [17]
#14 Comment By flo On August 6, 2015 @ 10:05
I tried out Matlab 2011b and JVM 1.8
The GUI ist starting and plots are shown well.
If I set the figure visible off, an then set the figure visible on, there are just the axes elements, but no buttons or anything else.
Anybody with an idea to fix that?
Thanks!!
#15 Comment By Yair Altman On August 6, 2015 @ 11:29
@Flo – R2011b uses JVM 1.6 by default – you’re trying to run it with a version that is 2 full generations newer, so I’m not surprised that some things simply don’t work. Try using 1.7, maybe you’ll have better luck…