Using UCanAccess in MathWorks MATLAB #42
|
Hello :) I'm using MacOS on M chip macbook. Ok so far I used UcanAccess 5.0.1 in Matlab 2024 and it worked fine. Now I upgraded to Matlab 2026 and there are problems now. Anyway I thought I give it a chance and try to upgrade to UcanAccess 5.1.5. So atm I'm including like this in Matlab When I then run which net.ucanaccess.jdbc.UcanaccessDriver -all afterwards it findes something net.ucanaccess.jdbc.UcanaccessDriver is a Java method % net.ucanaccess.jdbc.UcanaccessDriver constructor However calling java.lang.Class.forName('net.ucanaccess.jdbc.UcanaccessDriver'); outputs that error So not really sure what is wrong. Java 17 and 21 Matching Java Virtual Machines (2): Both are not working. So I'm not really sure if one of you is using Matlab or has an idea what goes wrong. It would be cool to get it working again otherwise I will stick to two different Matlab versions which will hopefully work. Thanks :) |
Replies: 2 comments 3 replies
|
thanks for the detailed report. The odd part is that Instead of adding all the individual JARs, please use the uber JAR which bundles all bytecode into a single jar file: https://repo1.maven.org/maven2/io/github/spannm/ucanaccess/5.1.5/ucanaccess-5.1.5-uber.jar This jar is essentially what used to be called That reduces the setup to a single If that doesn't help, you could also try adding the JAR to MATLAB's static classpath via I am not too familiar with MATLAB. Do keep us updated whether you get it working! cheers |
|
Hi Jens @Steineklopfer, I had the opportunity to play with MATLAB and after quite some trial-and-error got some results. Using UCanAccess in MathWorks MATLABTo successfully use UCanAccess in MATLAB (especially with modern versions and Java 11+), it is recommended to bypass the UCanAccess requires at least Java 11. I set up an Eclipse Temurin Java 17 JRE in MATLAB and tested under Windows 11. Test Script% Environment Information
fprintf('MATLAB Version: %s\n', version);
fprintf('Java Version: %s\n', version('-java'));
fprintf('jenv Status:\n');
disp(jenv);
% Base directory
basePath = 'C:\Temp\UCanAccess\';
% Absolute path to the sample database
dbPath = fullfile(basePath, 'ucanaccess.accdb');
% Add the UCanAccess Uber-JAR to MATLAB's dynamic Java class path
javaaddpath(fullfile(basePath, 'ucanaccess-5.1.5-uber.jar'));
dbUrl = ['jdbc:ucanaccess://' dbPath];
% initialize variables for cleanup
conn = [];
stmt = [];
rs = [];
try
% Explanation why the DriverManager lines below fail:
% MATLAB's `javaaddpath` loads JAR files into a *dynamic* ClassLoader.
% However, Java's built-in `java.sql.DriverManager` resides in the system
% ClassLoader and strictly refuses to load drivers from a different
% (dynamic) ClassLoader for security reasons.
%
% java.lang.Class.forName('net.ucanaccess.jdbc.UcanaccessDriver');
% conn = java.sql.DriverManager.getConnection(dbUrl);
% Workaround: By instantiating the driver explicitly, we bypass the
% DriverManager and its ClassLoader restrictions entirely:
driver = net.ucanaccess.jdbc.UcanaccessDriver();
props = java.util.Properties();
conn = driver.connect(dbUrl, props);
% Execute query
stmt = conn.createStatement();
rs = stmt.executeQuery('SELECT id, txt FROM t_sample');
% Pre-allocate data storage
data = {};
rowIdx = 1;
% Iterate through result set
while rs.next()
% Append directly as a new row (1x2 cell array)
data(end+1, :) = {rs.getInt('id'), char(rs.getString('txt'))};
end
% Process results
% Convert cell array to matlab table for easier analysis
if ~isempty(data)
resultTable = cell2table(data, 'VariableNames', {'id', 'txt'});
disp(resultTable);
else
fprintf('Log: No data found in table t_sample\n');
end
catch ex
fprintf('Error: Database operation failed: %s\n', ex.message);
end
% Resource cleanup (simulating finally block)
% Wrapped in a try-catch to prevent MATLAB from crashing during cleanup,
% which could leave the .laccdb file locked.
try
if ~isempty(rs), rs.close(); end
if ~isempty(stmt), stmt.close(); end
if ~isempty(conn), conn.close(); end
catch
% Ignore cleanup errors silently
endSuccessful execution in MATLAB R2025a
Related Resources |

Hi Jens @Steineklopfer,
I had the opportunity to play with MATLAB and after quite some trial-and-error got some results.
Using UCanAccess in MathWorks MATLAB
To successfully use UCanAccess in MATLAB (especially with modern versions and Java 11+), it is recommended to bypass the
DriverManagerif you are usingjavaaddpath(dynamic classpath), as this can lead to ClassLoader issues. Instead, instantiate the driver directly.UCanAccess requires at least Java 11. I set up an Eclipse Temurin Java 17 JRE in MATLAB and tested under Windows 11.
Test Script