MATLAB: Can I load Java classes into MATLAB R2020b using Maven

MATLAB

There is a Java package which I want to be able to use in MATLAB. The developers for this package refer to Maven when it comes to adding this package to your projects. Now on Maven Central Repository I was able to download the JAR-file for the package but it appears this really only contains the package itself without all its dependencies; so just adding that to MATLAB then did not work. It appears that I manually need to go through and download all these dependencies (and their dependencies, etc.) separately and then add all those dependencies on the Java classpath in MATLAB. Is there an easier way to do this? Can I provide a Maven <dependency> node to MATLAB and then have MATLAB resolve all dependencies automatically.

Best Answer

The ability to load Java classes into MATLAB based on a Maven <dependency> definition is not available in MATLAB R2020b. You can however actually use Maven itself outside of MATLAB to have it download all dependencies for you or possibly even package the main Java package and all its dependencies into a single JAR-file which can then be added to MATLAB.
Download all dependencies
1. Download/Install Maven (and its dependencies like a supported JDK if needed), see:
https://maven.apache.org/
2. Create a pom.xml file for your "project", for example:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myProject</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.8.1</version>
</dependency> </dependencies>
</project>
Where you can further customize the properties of this project itself (although they are not really relevant in this workflow):\n
<groupId>com.example</groupId>
<artifactId>myProject</artifactId>
<version>1.0.0</version>
And you customize the <dependencies> to actually refer to the Java package which you want to load into MATLAB, in this example we refer to Microsoft's MSAL4J package:\n
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.8.1</version>
</dependency>
3. Now in the directory where you created your pom.xml run Maven's copy-dependencies command of the dependency plugin:\n
mvn dependency:copy-dependencies -DoutputDirectory=deps
Which will download all the dependencies into a directory named "deps". You can now add all the downloaded JAR-files to MATLAB's Java classpath.
Package the Java classes and all its dependencies into a single JAR
1. Download/Install Maven (and its dependencies like a supported JDK if needed), see:
https://maven.apache.org/
2. Create a pom.xml file for your "project", for example:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>msal4j</artifactId>
<version>1.8.1</version>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.8.1</version>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
Where you can further customize the properties of this project itself (this will influence the name of the JAR-file which is produced):\n
<groupId>com.example</groupId>
<artifactId>myProject</artifactId>
<version>1.0.0</version>
And you customize the <dependencies> to actually refer to the Java package which you want to load into MATLAB, in this example we refer to Microsoft's MSAL4J package:\n
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>msal4j</artifactId>
<version>1.8.1</version>
</dependency>
3. Now in the directory where you created your pom.xml run:\n
mvn clean assembly:single
Which will download all the dependencies, clean the project and then assemble all relevant classes into a single JAR-file. You can then add this JAR-file to your MATLAB Java classpath.