覆蓋 MATLAB 默認靜態 javaclasspath 的最佳方法 (Best way to override MATLAB's default static javaclasspath)


問題描述

覆蓋 MATLAB 默認靜態 javaclasspath 的最佳方法 (Best way to override MATLAB's default static javaclasspath)

MATLAB is configured to search its static java class path before searching the user‑modifiable dynamic path. Unfortunately, the static path contains quite a number of very old public libraries, so if you are trying to use a new version you may end up loading the wrong implementation and get errors. 

For instance, the static path contains an old copy of the google‑collections.jar, which has long been supplanted by Google's guava library and which has some of the same class names (e.g. com.google.common.base.Objects). As a result, if you invoke a Guava method that uses a newer method of one of such a class, you will end up getting surprising NoSuchMethodErrors because the google‑collections jar is found first.

As of R2012b, MATLAB lets you specify additional jars to add to the static path by putting a javaclasspath.txt file in your preferences folder, but that adds jars to the end of the path, and doesn't let you override jars that are built into MATLAB.

So what is the best way around this?


參考解法

方法 1:

I got an official response from Mathworks:

As of MATLAB R2013a (also in R2012b), classes can be added to the front of the static Java class path by including the following line in javaclasspath.txt:

<before>

Any directory that is after this line in javaclasspath.txt will be added to the front of the static Java class path. This is an undocumented use of javaclasspath.txt as of R2013a.

But overall in MATLAB, the ability to add classes to the front of the static Java classpath is not available through javaclasspath.txt in MATLAB 8.0 (R2012b).

MATLAB searches for classpath.txt in the following order:

  1. In the startup directory. As of MATLAB 8.0 (R2012b) a warning will be shown if the file is found there and it will be ignored.

  2. In the first directory on the MATLABPATH environment variable. (This environment variable is used in the bin/matlab shell script on Linux and in general is not used by the end‑user).

  3. In the toolbox/local directory.

Although the MATLABPATH environment variable of point 2 is normally not used by end‑users we can use it in a workaround to allow reading a custom classpath.txt outside of the toolbox/local directory.

On Windows:

You will need to create the MATLABPATH environment variable. The first directory on it should be your directory with the custom classpath.txt AND you will also need to add the toolbox\local directory as second option. So from a cmd prompt you could do:

  

set MATLABPATH=c:\Users\user\Documents\myMATLABClasspath;c:\Program Files\MATLAB\R2012b   \toolbox\local   matlab.exe

方法 2:

One hack that appears to work is to add the jar to the top of the classpath.txt file that can be found in your MATLAB installations toolbox/local folder. Unfortunately, this is automatically generated and may get rewritten at some unspecified time, such as when you install new toolboxes, so this approach would require you to have some way to notice when this happens and reapply the hack.

方法 3:

If you're distributing a jar that's intended to be used with matlab, it may be better to use proguard as described at http://code.google.com/p/guava‑libraries/wiki/UsingProGuardWithGuava.

If you specify that all of your classes and their (public) fields and methods are to be preserved and include guava as a program jar (not a library), then it will rename all of guava's methods and update your compiled bytecode to reference the new names.

It seems a bit hackish, but depending on the audience, it may be significantly easier than teaching your users about static vs. dynamic classpath, and it won't break any matlab code that depends on the old behavior.

方法 4:

Instead of obfuscating the package as suggested by @user2443532, I have found it easier to "shade" the conflicting package instead of obfuscating it ‑ unless you actually need obfuscation. One easy way to do this is to build your package using Maven and use the maven‑shade‑plugin. Internal calls are modified automatically, so you don't need to modify any of the Java code.

Direct calls from Matlab will need to be modified ‑ for example, calls to com.opensource.Class become shaded.com.opensource.Class.

For more info on shading, see What is the maven‑shade‑plugin used for, and why would you want to relocate Java packages?

(by Christopher BarberChristopher BarberChristopher Barberuser2443532Phil)

參考文件

  1. Best way to override MATLAB's default static javaclasspath (CC BY‑SA 3.0/4.0)

#matlab #java #classpath






相關問題

MATLAB:多線程和多核之間的區別 (MATLAB: difference between Multithreading and Multicore)

在 matlab 中用 imread 讀取圖像文件會給出什麼樣的表示? (reading a image file with imread in matlab gives what kind of representation?)

Як прызначыць індывідуальныя пазнакі значэнняў на восях у Matlab? (How to assign individual labels to values on the axes in Matlab?)

覆蓋 MATLAB 默認靜態 javaclasspath 的最佳方法 (Best way to override MATLAB's default static javaclasspath)

如何在 Matlab 中為這個 3D 繪圖設置動畫? (How to animate this 3D plot in Matlab?)

如何轉換 x = [x_de,x_nu]; 從matlab到python? (How do I convert x = [x_de,x_nu]; from matlab to python?)

opnet 和 matlab 接口 (opnet and matlab interfacing)

Matlab:使用正非整數邊緣權重執行最小切割 (Matlab: perform min cut with positive non-integer edge weights)

帶矩陣的圖像疊加 (Image overlay with matrix)

在矩形 [a,b]x[c,d] 上離散最小二乘逼近。使用函數 1,x,y,sin(x) 和 sin(y) 作為基礎 (Discete least sqauare approximation on rectangle [a,b]x[c,d].Use functions 1,x,y,sin(x) and sin(y) as the basis)

如果這個變量在調用之間發生了變化,為什麼匿名函數的固定參數沒有更新? (Why is fixed argument of anonymous function not updated, if this variable has changed between calls?)

在 MATLAB 中通過 HTTP 接收數據 (Receive data via HTTP in MATLAB)







留言討論