calling java method from 2012b is not working

3 views (last 30 days)
We have a program that will call external java method and it works on matlab 2013/2014, but it does not work on 2012b. After initialize the class, it cannot find any methods defined in it.
if we do this:
obj = TestJavaClass(); // initial the object, ok
objClass = obj.getClass(); // get the class information of the obj, ok
objClass.getDeclaredMethods() // trying to get the defined methods, exception below
the last call will trigger the following exception and the error is false since the code did not override a final method at all.
Java exception occurred:
java.lang.VerifyError: class
com.lgc.bulkdata.SeismicDataHeaderBuf$SeismicDataHeader overrides final
method getUnknownFields.()Lcom/google/protobuf/UnknownFieldSet;
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at
java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
at java.lang.Class.getDeclaredMethods(Class.java:1845)
the java code is compiled with jdk 1.6.0_27, I noticed that the JAVA version in 2012b is 1.6.0_17 and in 2014 it is 1.7.
Since our client cannot upgrade to 2014 now, is there any workaround of the issue?
thanks kevin
  2 Comments
Florian Enner
Florian Enner on 26 Sep 2014
I had the same problem and have debugged this a bit further. The error is caused by incompatibilities between Google Protocol Buffer versions. Essentially you are trying to run protobuf >2.5.0 messages on a 2.4.1 (or lower) protobuf library.
This is caused by some versions of Matlab shipping with an internal version of Protobuf, which to the best of my knowledge can't be overridden. You can execute the following to check whether your version ships with protobuf,
methods('com.google.protobuf.AbstractMessage')
  • 2009a: not included
  • 2012b: included
  • 2013a: included
I can think of 2 ways to fix this. Both of them require access to source code.
  • Use an old version of protobuf that is compatible. Note that this will break if future Matlab releases upgrade Protobuf.
  • Fork protobuf and change the namespace. This option is terrible, but may be the only one that will reliably work with all Matlab releases.
Can someone from Mathworks please comment on which protobuf versions are bundled into the various Matlab releases? I'd really like to avoid forking if at all possible.
thanks,
Florian
Florian Enner
Florian Enner on 30 Apr 2015
In case someone else comes across this issue:
I eventually ended up using the Maven Shade plugin to relocate external dependencies into non-colliding namespaces, e.g.,
<relocation>
<pattern>com.google</pattern>
<shadedPattern>my.company.shaded.google</shadedPattern>
</relocation>
- Florian

Sign in to comment.

Answers (0)

Categories

Find more on Java Package Integration in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!