MATLAB: Custom ROS message triggers ClassNotFoundException even though it shows up in rosmsg list

MATLABros

I'm currently playing around with custom ROS message definitions in MATLAB R2020a.
I generated my custom messages following the tutorial. They successfully build without any problems and show up when using rosmsg list after restarting MATLAB.
However, when I now try to read the message from a topic in a ROS bag file, the class definitions can't be found, prompting the following error:
Error using ros.BagSelection/deserializeMessages (line 641)
Java exception occurred:
org.ros.exception.RosMessageRuntimeException: java.lang.ClassNotFoundException:
custom_msgs.ObjectList
at
org.ros.internal.message.definition.MessageDefinitionReflectionProvider.get(MessageDefinitionReflectionProvider.java:66)
at
org.ros.internal.message.DefaultMessageFactory.newFromType(DefaultMessageFactory.java:42)
at
org.ros.internal.message.DefaultMessageDeserializer.deserialize(DefaultMessageDeserializer.java:42)
Caused by: java.lang.ClassNotFoundException: custom_msgs.ObjectList
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at
org.ros.internal.message.definition.MessageDefinitionReflectionProvider.get(MessageDefinitionReflectionProvider.java:62)
... 2 more
Error in ros.BagSelection/deserializeMessages (line 641)
msg{i,1} = ros.msg.internal.MessageFactory.newFromJava(msgType,
des.deserialize(cbuffer));
Error in ros.BagSelection/readMessages (line 278)
msgs = obj.deserializeMessages(obj.MessageList, rows);
Error in TestClass/visualizeData (line 77)
data = readMessages(topic, i);
When trying to retrieve the message defintion with rosmsg show custom_msgs.ObjectList, the same problem occurs:
Could not find definition of message type custom_msgs/ObjectList. Check the spelling of
the type, or use "rosmsg list" to retrieve a list of available message types.
ObjectList.msg is defined as following:
std_msgs/Header header # Time stamp for the object list is in the header
ObjectData[] objects # List of objects

Best Answer

Adding the path of your generated messages files to javaclasspath.txt (as indicated in the command window when generating the messages) fixed the problem.