I\'m using MATLAB R2007b, Java 1.6 SE, Eclipse Helios, and MySql 5 on Windows XP Pro SP3.
I\'m trying to create a class library that uses JPA annotations to access a
Ensure that you have a JPA provider jar (such as eclipselink.jar) on your classpath.
Well, I found "an answer." Somewhere before I saw a post about the difference in MATLAB's "dynamic" and "static" cp's. The "static" cp is a text file that is loaded at startup. The "dynamic" cp is loaded at runtime and you typically manipulate it with m-script calls. That's what I was trying to do.
So I added my jars to the dynamic path and it didn't work.
I added them to the end of the static path and got DIFFERENT errors, which seemed to pertain to the XML parsing. Progress!
Then I added my jars to the BEGINNING of the static path and it works.
To quote Bart Simpson: Craptackular.
Thanks for all your ideas. Ask me a C# question so I can reciprocate...
-reilly.
While working with Java in MATLAB, I often encountered issues with the dynamic classpath. As a workaround, using classpath.txt
has solved any issue so far.
Dealing with different environments, for example testing and production, results in multiple classpath.txt
files in your MATLAB start directory. Using different MATLAB releases adds another multiplier to the number of classpath.txt
files around.
ClassPathHacker.java is an option to add dynamically classes and jar files to your static classpath. Using this approach there is no need to touch classpath.txt
anymore. Your Java classpath configuration can stay in the intended place startup.m
.
This is just a followup to your answer about static vs dynamic classpaths. Here's a function which will let you diagnose where a Java class is being loaded from within Matlab, and if there's any masking of the class definitions, which could be why it was sensitive to ordering for you. You might see other collisions; at least dom4j.jar and commons-collections.jar are shipped with Matlab, but I don't know what versions.
function whereisjavaclassloadingfrom(ClassName)
%WHEREISJAVACLASSLOADINGFROM Show where a Java class is loaded from
%
% whereisjavaclassloadingfrom(ClassName)
%
% Shows where a Java class is loaded from in this Matlab session's JVM.
% This is for diagnosing Java class load problems, such as classpath
% ordering issues, seeing if a class of a given name is included in an
% unexpected JAR file, etc.
%
% Displays output to console.
%
% Examples:
%
% whereisjavaclassloadingfrom('java.util.HashMap')
% whereisjavaclassloadingfrom('com.ldhenergy.etools.MxUtil')
% whereisjavaclassloadingfrom('com.google.common.collect.Maps')
% whereisjavaclassloadingfrom('org.apache.commons.math.complex.Complex')
% Use javaArray to get Class object without having to instantiate. This
% lets it work with objects that have private or non-zero-arg constructors,
% and avoids side effects of object construction.
% (Would use java.lang.Class.forName(), because that's a more direct way of
% doing this, but it doesn't work for stuff on the dynamic classpath.)
ja = javaArray(ClassName,1);
klass = ja.getClass().getComponentType();
klassLoader = klass.getClassLoader();
if isempty(klassLoader)
% JVM used null to represent the "bootstrap" class loader
% I think that's the same as the "system" class loader
klassLoader = java.lang.ClassLoader.getSystemClassLoader();
end
klassLoaderStr = char(klassLoader.toString());
klassFilePath = [strrep(ClassName, '.', '/') '.class'];
try
% This logic assumes that the classes exist as files in the class
% loader. It's a valid assumption for mainstream class loaders,
% including the one's I've seen with Matlab.
klassUrl = klassLoader.getResource(klassFilePath);
if isempty(klassUrl)
klassUrlStr = '';
else
klassUrlStr = char(klassUrl.toString());
end
catch err
klassUrlStr = sprintf('ERROR: %s', err.message);
end
% Get all locations, to reveal masked definitions
urls = enumeration2array(klassLoader.getResources(klassFilePath));
disp(sprintf('Version: %s\nClass: %s\nClassLoader: %s\nURL: %s', version,...
char(klass.getName()), klassLoaderStr, klassUrlStr));
if numel(urls) > 1
disp('Class is masked:');
for i = 1:numel(urls)
disp(sprintf('URL %d: %s', i, char(urls(i))));
end
end
%%
function out = enumeration2array(jenum)
tmp = {};
while jenum.hasMoreElements()
tmp{end+1} = jenum.nextElement();
end
out = [tmp{:}];
Are you absolutely certain that you have spelled the name of the persistence unit correctly in the call to:
javax.persistence.Persistence.createEntityManagerFactory(String puName)
That would also give you the same error. The name is case-sensitive.