MATLAB Answers

External monitor throws Java exception

88 views (last 30 days)
When I connect/disconnect my external monitor, a Java exception appears on MATLAB's command line:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.BufImgSurfaceData cannot be cast to sun.java2d.xr.XRSurfaceData at sun.java2d.xr.XRPMBlitLoops.cacheToTmpSurface( at sun.java2d.xr.XrSwToPMBlit.Blit( at sun.java2d.pipe.DrawImage.blitSurfaceData( at sun.java2d.pipe.DrawImage.renderImageCopy( at sun.java2d.pipe.DrawImage.copyImage( at sun.java2d.pipe.DrawImage.copyImage( at sun.java2d.pipe.ValidatePipe.copyImage( at sun.java2d.SunGraphics2D.drawImage( at sun.java2d.SunGraphics2D.drawImage( at javax.swing.ImageIcon.paintIcon( at com.mathworks.toolstrip.plaf.GalleryButtonUI.paintContent( at com.mathworks.toolstrip.plaf.RegularButtonUI.paintContent( at com.mathworks.toolstrip.plaf.ToolstripButtonUI.paint( at javax.swing.plaf.ComponentUI.update( at javax.swing.JComponent.paintComponent( at com.mathworks.mwswing.MJButton.paintComponent( at javax.swing.JComponent.paint( at com.mathworks.mwswing.MJButton.paint( at javax.swing.JComponent.paintChildren( at javax.swing.JComponent.paint( at javax.swing.JComponent.paintChildren( at javax.swing.JComponent.paint( at javax.swing.JComponent.paintChildren( at javax.swing.JComponent.paint( at javax.swing.JComponent.paintToOffscreen( at javax.swing.BufferStrategyPaintManager.paint( at javax.swing.RepaintManager.paint( at javax.swing.JComponent._paintImmediately( at javax.swing.JComponent.paintImmediately( at javax.swing.RepaintManager$ at javax.swing.RepaintManager$ at Method) at$JavaSecurityAccessImpl.doIntersectionPrivilege( at javax.swing.RepaintManager.paintDirtyRegions( at javax.swing.RepaintManager.paintDirtyRegions( at javax.swing.RepaintManager.prePaintDirtyRegions( at javax.swing.RepaintManager.access$1200( at javax.swing.RepaintManager$ at java.awt.event.InvocationEvent.dispatch( at java.awt.EventQueue.dispatchEventImpl( at java.awt.EventQueue.access$500( at java.awt.EventQueue$ at java.awt.EventQueue$ at Method) at$JavaSecurityAccessImpl.doIntersectionPrivilege( at java.awt.EventQueue.dispatchEvent( at java.awt.EventDispatchThread.pumpOneEventForFilters( at java.awt.EventDispatchThread.pumpEventsForFilter( at java.awt.EventDispatchThread.pumpEventsForHierarchy( at java.awt.EventDispatchThread.pumpEvents( at java.awt.EventDispatchThread.pumpEvents( at
I'm using MATLAB R2017b (fresh install) on Xubuntu 17.10. I'm invoking MATLAB via the command
LD_PRELOAD=/usr/lib/x86_64-linux-gnu/ /usr/local/MATLAB/R2017b/bin/matlab -desktop
Since this doesn't crash the MATLAB GUI, I don't consider it a critical issue, but it is annoying (and easily reproduced).
Edit: Arguably this does 'crash' the MATLAB GUI: subsequent keyboard input retriggers the same exception. My workaround, which I forgot to mention, is mouse-clicking the Clear Workspace button. I won't pretend to understand how this resolves the issue.


Show 6 older comments
Nathan Ullberg
Nathan Ullberg on 12 Oct 2018
Similar issue for me: R2018b, Ubuntu 18.04.
Richard Crozier
Richard Crozier on 18 Oct 2018
R2018b Ubuntu 18.04, also still get same issue
Jo Williams
Jo Williams on 13 Feb 2020
...Same here. I'm operating through NX, and it seems to be when the NX window is resized that matlab triggers all this.
MATLAB Version: (R2018b)
Operating System: Linux 3.10.0-1062.12.1.el7.x86_64 #1 SMP Tue Feb 4 23:02:59 UTC 2020 x86_64
Java Version: Java 1.8.0_152-b16 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode

Sign in to comment.

Accepted Answer

Andrew King
Andrew King on 17 Apr 2018

Seems like I've fixed my problem by disabling xrender by putting the line


in a new file called /usr/local/MATLAB/R2018a/bin/glnxa64/java.opts

Unintended consequences TBD.

Java options ref:

Similar bug:


Nick Knight
Nick Knight on 18 Apr 2018
I am accepting this answer as it appears to have resolved my issue, at least for R2018a on Ubuntu 17.10, and the only risk seems to be a possible performance hit: sun.java2d.xrender: [...] enable the XRender-based Java 2D rendering pipeline for modern X11-based desktops, offering improved graphics performance
Guillaume R
Guillaume R on 13 Nov 2018
I tried that solution on Ubuntu 18.04 and with Matlab R2018b. I don't know if it solved it, but it made the graphics performance so low and laggy that it was unusable, so I removed it. Would be nice to have an official fix provided by Mathworks.

Sign in to comment.

More Answers (1)

Robert on 13 Nov 2018
I had the same issue with R2018a on Xubuntu 16.04 and 18.04 operated remotely via X2Go. Thanks for the solution, works fine! This answer should definitely be placed more prominently, e.g. in the FAQ. Took me a while to find it..

  1 Comment

Pinocchio on 28 Apr 2019
Similar issue for me: R2018b, Ubuntu 18.04.

Sign in to comment.



Community Treasure Hunt

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

Start Hunting!