Comments on: Java class access pitfalls https://undocumentedmatlab.com/articles/java-class-access-pitfalls?utm_source=rss&utm_medium=rss&utm_campaign=java-class-access-pitfalls Professional Matlab consulting, development and training Thu, 20 Oct 2016 17:29:42 +0000 hourly 1 https://wordpress.org/?v=6.7.3 By: Yair Altman https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-391136 Thu, 20 Oct 2016 17:29:42 +0000 http://undocumentedmatlab.com/?p=4881#comment-391136 In reply to Amaury Vanvinckenroye.

@Amaury – this seems to be something specific to the MongoClient class. You should probably ask about this in the MongoDB forums.

]]>
By: Amaury Vanvinckenroye https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-391039 Wed, 19 Oct 2016 11:26:30 +0000 http://undocumentedmatlab.com/?p=4881#comment-391039 Hi, I’m using some Java classes in a Matlab code and it seems that a specific java class is not loaded, but only at the first run (i.e. at the start of Matlab). Later calls to the java function don’t give any error, even if I “clear all; clear classes; clear java”. Here’s the part of code failing at first run.

javaaddpath(Obj.MongoDBPath)
import java.util.ArrayList;
import java.lang.String;
import com.mongodb.*;

credentialsList = ArrayList();
credentials = MongoCredential.createCredential(Username, 'admin' , password);
credentialsList.add(credentials);
mongoClient = MongoClient(ServerAddress(IP,Port), credentialsList);

The failing java class is ServerAdress and is part of the mongodb package.
Other java classes run fine immediately.
This error doesn’t occur on all computers, so I’m giving you my specs.
Windows 10 (previously windows 8), Matlab R2013a with Java 1.6.0_17-b04 (also tried with Java 1.8.0_25-b18 when adding it as environment variable).
It worked on a Mac with Matlab R2015b with Java 1.7, but also failed on another Mac with Matlab R2014a and java 1.8.
Thanks in advance

]]>
By: Malcolm Lidierth https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-372061 Wed, 16 Mar 2016 20:26:33 +0000 http://undocumentedmatlab.com/?p=4881#comment-372061 In reply to Vincent.

The class loader will only load one copy of a specific package/class combination. A newer version will not replace the older one even if you have called javaaddpath (hence the NoSuchFieldError which implies you have created an instance using the old code). This is a Java class loader feature: if you load a jar that has dependencies on other jars (e.g. in a /dist/lib folder), and some of those jars are already loaded, the already loaded versions will continue to be used.

In general, open source projects with the Apache brand will maintain backwards compatibility – deprecating, but not removing, old fields/methods. If backwards compatibility is not maintained Apache projects will usually alter the package names so both versions can be used simultaneously. This being the case, replacing the jar file shipped with MATLAB with the newer version should produce no problems. But, anyone else using your code will need to do that too. Buyer beware.

]]>
By: Vincent https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-372056 Wed, 16 Mar 2016 18:38:13 +0000 http://undocumentedmatlab.com/?p=4881#comment-372056 How about what to do when you get a “java.lang.NoSuchFieldError” because your java code uses a version of Apache’s httpcore that is newer than the one matlab ships with, and the old one seems to be creating conflicts with the newer one you’ve added to your java path?

]]>
By: Yair Altman https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-348751 Sat, 02 May 2015 17:18:06 +0000 http://undocumentedmatlab.com/?p=4881#comment-348751 In reply to waldroje21.

If you wish me to help you debug your specific installation, contact me by mail for a private consulting.

]]>
By: waldroje21 https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-348717 Fri, 01 May 2015 20:21:47 +0000 http://undocumentedmatlab.com/?p=4881#comment-348717 In reply to Yair Altman.

in regard to the first… it shows up on the bottom when I type javaclasspath in the workspace, so I assume it can’t be the wrong file, and that path is what i get when i type prefdir… I definitely restarted Matlab…. and I’m using the same exact file string in the Dynamic path, that I use in the javaclasspath.txt, i just copied it straight from terminal. I’ve done this with no problem on 3 other installs, so it’s a bit strange.

]]>
By: Yair Altman https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-348707 Fri, 01 May 2015 15:26:58 +0000 http://undocumentedmatlab.com/?p=4881#comment-348707 In reply to waldroje21.

Perhaps one or more of the following happened:

* you edited the wrong classpath.txt file
* you forgot to restart Matlab after editing the classpath.txt file
* you mistyped the class’s full path in the file
* you used a relative path in the file

]]>
By: waldroje21 https://undocumentedmatlab.com/articles/java-class-access-pitfalls#comment-348706 Fri, 01 May 2015 15:22:08 +0000 http://undocumentedmatlab.com/?p=4881#comment-348706 Any idea why a Matlab would recognize a Java class file from the Dynamic path, but not the Static? I’ve added it to my Static path, and it’s in there… but does not work… when I then add the same path using javaaddpath , it works.

]]>