# Static Java classpath hacks

A few days ago I encountered a situation where I needed to incorporate a JAR file into Matlab’s static Java classpath. I came across several relevant hacks that I thought could be useful for others:

### The documented approach

In most use-cases, adding Java classes (or ZIP/JAR files) to the dynamic Java classpath is good enough. This can easily be done in run-time using the javaaddpath function. In certain cases, for example where the Java code uses asynchronous events, the Java files need to be added to the static, rather than the dynamic, Java classpath. In other cases, the Java code misbehaves when loaded using Matlab’s dynamic Java classloader, rather than the system classloader (which is used for the static classpath (some additional info). One example of this are the JAR/ZIP files used to connect to various databases (each database has its own Java JDBC connector, but they all need to reside in the static Java classpath to work properly).

Adding class-file folders or ZIP/JAR files to the static Java classpath can be done in several manners: we can update the Matlab installation’s classpath.txt file, or (starting in R2012b) a user-prepared javaclasspath.txt file. Either of these files can be placed in the Matlab (or deployed application’s) startup folder, or the user’s Matlab preferences folder (prefdir). This is all documented.

There are several important drawbacks to this approach:

### Parting words

How risky is all this in terms of future compatibility? well, I’m not a prophet, but I’d say that using these hacks is pretty risky in this regard. Matlab’s custom classloader is way-deep in undocumented territory. This does not mean that the functionality will change tomorrow, but don’t be surprised if and when it does.

Have you ever used any additional undocumented hack relating to using the Java classpaths in Matlab? If so then please post a comment below.

Users who have endured my post this far, should probably also read my related article about Java class access pitfalls.

 Print

### 3 Responses to Static Java classpath hacks

1. sebastian says:

Specifying relative paths in classpath.txt may not be supported, but for me it works just fine in R2011b.

Simply add paths relative to the location of classpath.txt (i.e. relative to matlabs startup directory):

# matlab's original classpath entries here
# ...

# your own stuff
myjars/myjar1.jar
myjars/myjar2.jar


The corresponding entries in the javaclasspath within matlab are not expanded properly, but all jars are properly loaded.

2. Ed Yu says:

Yair,

Ed.

3. Krishnan says:

Yair,

I think I have a method of adding and removing from static class path, but I am unable to make it stick on subsequent restarts of Matlab. The idea is to use java.lang.setProperty and java.lang.getProperty to set and edit the static class path.
1. Get the string from static class path and store it as a char

in_string=char(java.lang.getProperty('java.class.path');

2. manipulate the string either through string concatenate (strcat), string replace (strrep)
3. put the string back into the java.class.path property

java.lang.setProperty('java.class.path',java.lang.String(in_string));

If you could help add the last bit where loaded class paths can be made permanent. This would be great.

On a secondary note thank you for sharing whereisclassloadingfrom from Andrew, it has saved so much time.

thanks
krishnan