Skip to content

Instantly share code, notes, and snippets.

@jecs
Last active March 29, 2019 15:35
Show Gist options
  • Save jecs/28c501a726fdb02acb798f0226480bcc to your computer and use it in GitHub Desktop.
Save jecs/28c501a726fdb02acb798f0226480bcc to your computer and use it in GitHub Desktop.
Fix for MATLAB R2018a error 'Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.BufImgSurfaceData cannot be cast to sun.java2d.xr.XRSurfaceData'

The issue pops up when you resize the desktop, as is commonplace when you use VNC across multiple machines.

A good workaround for this issue is to install the latest Oracle Java 8.

Installing Oracle Java 8

If you are using Ubuntu, you can add the Oracle 8 ppa:

sudo add-apt-repository ppa:webupd8team/java
sudo apt update
sudo apt install oracle-java8-installer

Setting MATLAB_JAVA

Once you have installed Java 8, we can now override the Java version that MATLAB uses. The following procedure is based on this MATLAB Answers post.

We need to find the directory of the JRE. If you installed using the ppa, then this directory should be /usr/lib/jvm/java-8-oracle/jre. Otherwise, you need to look for the directory of the java installation such that in the subdirectory lib, you can find the file rt.jar.

As long as the environment variable MATLAB_JAVA is set to that directory, you should be able to start MATLAB with the newer version of java.

In my case, I created a simple bash script that runs MATLAB with the environment variable set:

#!/bin/bash
MATLAB_JAVA="/usr/lib/jvm/java-8-oracle/jre" \
/usr/local/bin/matlab "$@"

You might have to change the location of the MATLAB executable. You should also make the file executable and optionally place it on your path.

Verification

When you run version -java in MATLAB, it should return the same information as java -version on the terminal, where java is the executable that you just installed. (It might not be your selected java version.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment